Help with Printing Array... Im supposed to print out so 9 elements show per line.
I need a quicker way to do this... As you can see i used for loops for each 9 elements but it will take me forever considering I have to do this about 4 times :/
public static void main(String[] args) {
int[] A = new int[123]; // array will store 123 values
int d = 0;
int swap = 0; // swap counter
int compare = 0; // compare counter
Random generator = new Random(2020); // creates instance of Random class
for (int i = 0; i < A.length; i++) {
d = generator.nextInt(999); // Maximum value declared is 999
A[i] = d;
}
for (int i=0; i<9; i++){
System.out.print(A[i] + " "); // Prints array of values generated by Random class
}
System.out.println("");
for (int i=10; i<19; i++){
System.out.print(A[i] + " "); // Prints array of values generated by Random class
}
System.out.println("");
for (int i=20; i<29; i++){
System.out.print(A[i] + " "); // Prints array of values generated by Random class
}
System.out.println("");
for (int i=30; i<39; i++){
System.out.print(A[i] + " "); // Prints array of values generated by Random class
}
Re: Help with Printing Array... Im supposed to print out so 9 elements show per line.
Did you have a question? Simply posting unformatted code does not make it easy to help you.
Re: Help with Printing Array... Im supposed to print out so 9 elements show per line.
Yeah I included it in title and before program... Theres 123 elements in the array and I have to print it out so that 9 elements are on each line... With the last row being less than nine of course... I need a quicker way to do it rather than what Im doing now
Re: Help with Printing Array... Im supposed to print out so 9 elements show per line.
I will just point out that you have still not asked a question- usually questions end in a question mark. You have simply said what you need, which is not a question.
Anyway, you might want to read up on the % operator.