I´m trying to create a 3 dimensional array which should have three horizontal boxes which two rows and 3 columns in each. As it is the output get me two vertical boxes with 3 rows and 3 columns, I have tried to change the integers around but it seems to just get me in deeper trouble.
And on top of this I´ve been trying to add all the integers in the array to get and print out the mean/average value but I can´t really find/understand how to do this.
int[][][] ett= {{ {10,12,14}, {16,18,20}, {22,24,26} },
{ {11,13,15}, {17,19,21}, {23,25,27} } };
//want this to print out as it looks, three "boxes" with two rows and three columns
for (int i= 0; i<ett.length;i++){
for (int j= 0; j<ett[i].length ;j++){
for (int k=0; k<ett[i][j].length;k++){
System.out.print(ett[i][j][k]+" ");
}
System.out.println();
}
System.out.println();
}
int medel=0;
//medel = all integers in array added, 10+12+14...
// divide medel with number of integers in array (18)
System.out.println("Medelvärdet: "+medel);
}//main
You only have to get rid of one println :
for (int i= 0; i<ett.length;i++){
for (int j= 0; j<ett[i].length ;j++){
for (int k=0; k<ett[i][j].length;k++){
System.out.print(ett[i][j][k]+" ");
}
// System.out.println(); remove this println
}
System.out.println();
}
and you'll get this :
10 12 14 16 18 20 22 24 26
11 13 15 17 19 21 23 25 27
You might want to add spaces between the blocks :
for (int i= 0; i<ett.length;i++){
for (int j= 0; j<ett[i].length ;j++){
for (int k=0; k<ett[i][j].length;k++){
System.out.print(ett[i][j][k]+" ");
}
System.out.print(" ");
}
System.out.println();
}
which will give you this:
10 12 14 16 18 20 22 24 26
11 13 15 17 19 21 23 25 27
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With