Everything up until the last sequence of instructions is correct (although poorly formatted).
Here is original:
int maxValue = 1;
System.out.println("
Max values in 2D array: ");
for (int i = 0; i < twodArray.length; i++) {
for (int j = 0; j < twodArray.length; j++)
if (twodArray[i][j] > maxValue) {
maxValue = twodArray[i][j];
}
System.out.println(maxValue);
}
Here is better version:
int maxValue = 0;
System.out.println("
Max values in 2D array: ");
for (int i = 0; i < twodArray.length; i++) {
for (int j = 0; j < twodArray[i].length; j++) {
if (twodArray[i][j] > maxValue) {
maxValue = twodArray[i][j];
}
}
System.out.println("Max value of row " + i + ": " + maxValue);
}
Look carefully and you'll see that I added the {
character after the second for-loop.
If you wanted to find total max, and minimize open and close curly-braces here is another version:
int maxValue = 0;
System.out.println("
Max values in 2D array: ");
for (int i = 0; i < twodArray.length; i++)
for (int j = 0; j < twodArray[i].length; j++)
if (twodArray[i][j] > maxValue)
maxValue = twodArray[i][j];
System.out.println("Maximum value: " + maxValue);
Good luck.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…