Search the Community
Showing results for tags 'for loop'.
-
What I want is to print values in array index 10, 20,30,40 ... likewise using for loop. class coolExample { public static void main(String[] args) { int anArray[]; int j; anArray= new int[10]; anArray[0]=10; anArray[1]=20; anArray[2]=30; anArray[3]=40; anArray[4]=50; anArray[5]=60; anArray[6]=70; anArray[7]=80; anArray[8]=90; anArray[9]=100; for(j=0;j<anArray.length;j++) { System.out.println(anArray); } } } Output I'm getting from running this is : 0,1,2,3,4....9 and I tried this for(j=0;j<anArray.length;j++) { anArray[j]=j; System.out.println(anArray[j]); Same result.
-
One of the assignments for my final was to write an application that asked the user to input 10 integers, and then separate them into 2 arrays, one to hold numbers below 50, and one for numbers higher than 50, then display both arrays. I had a little fun with the for loop. Don't make fun of my shitty code. import java.util.Scanner; public class ListsOfIntegers { public static void main(String[] args) { Scanner input = new Scanner(System.in); int arrayOfInt[] = new int[10]; final int NUMBEROFINT = 10; System.out.println("Enter an integer"); for(int x = 0; x < NUMBEROFINT; x++) { arrayOfInt[x] = input.nextInt(); System.out.println("Enter another integer"); } int counter = 0; for(int y = 0; y < NUMBEROFINT; y++) { if (arrayOfInt[y] > 50) { counter++; } } int secArray[] = new int[counter]; int arrayCounter = 0; for(int j = 0; j < NUMBEROFINT; j++) { if (arrayOfInt[j] > 50) { secArray[arrayCounter] = arrayOfInt[j]; arrayCounter++; } } int counter2 = 0; for(int i = 0; i < NUMBEROFINT; i++) { if (arrayOfInt[i] < 50) { counter2++; } } int thirdArray[] = new int[counter2]; arrayCounter = 0; for(int k = 0; k < NUMBEROFINT; k++) { if (arrayOfInt[k] < 50) { thirdArray[arrayCounter] = arrayOfInt[k]; arrayCounter++; } } System.out.println("All numbers above 50"); for(int b = 0; b < counter; b++) { System.out.print(secArray[b] + " "); } System.out.println("\n\nAll numbers below 50"); for(int c = 0; c < counter2; c++) { System.out.print(thirdArray[c] + " "); } System.out.println("=F|A= FTW"); } }
About Us
We are glad you decided to stop by our website and servers. At Fearless Assassins Gaming Community (=F|A=) we strive to bring you the best gaming experience possible. With helpful admins, custom maps and good server regulars your gaming experience should be grand! We love to have fun by playing online games especially W:ET, Call of Duty Series, Counter Strike: Series, Minecraft, Insurgency, DOI, Sandstorm, RUST, Team Fortress Series & Battlefield Series and if you like to do same then join us! Here, you can make worldwide friends while enjoying the game. Anyone from any race and country speaking any language can join our Discord and gaming servers. We have clan members from US, Canada, Europe, Sri Lanka, India, Japan, Australia, Brazil, UK, Austria, Poland, Finland, Turkey, Russia, Germany and many other countries. It doesn't matter how much good you are in the game or how much good English you speak. We believe in making new friends from all over the world. If you want to have fun and want to make new friends join up our gaming servers and our VoIP servers any day and at any time. At =F|A= we are all players first and then admins when someone needs our help or support on server.