Print largest number in a 2d array - why do my code print three numbers(打印二维数组中的最大数字 - 为什么我的代码打印三个数字)
问题描述
我正在尝试打印二维数组中的最大数字.我的问题是我的输出是三个数字而不是一个 - 最大的.为什么?
I am trying to print out the largest number in a 2D array. My problem is that my output are three numbers instead of one - the largest. Why?
这是我的代码:
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int maxRows = 3;
int maxCols = 4;
int [] onedArray = new int [maxRows];
for (int i = 0; i < maxRows; i++){
onedArray[i] = (int) ((Math.random() * 100) * maxCols);
}
int [][] twodArray = new int[maxRows][];
for (int i = 0; i < maxRows; i++){
twodArray[i] = new int[maxCols];
}
for (int i = 0; i < twodArray.length; i++){
for (int j = 0; j < twodArray[i].length; j++){
twodArray[i][j] = (int) (Math.random() * 100);
}
}
System.out.println("2 - The 2D array: ");
for (int i = 0; i < twodArray.length; i++){
for (int j = 0; j < twodArray[i].length; j++){
System.out.print(twodArray[i][j] + " ");
}
System.out.println("");
}
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);
}
}
}
推荐答案
直到最后一个指令序列为止的一切都是正确的(尽管格式很差).
Everything up until the last sequence of instructions is correct (although poorly formatted).
这是原文:
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);
}
这里有更好的版本:
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);
}
仔细看,你会发现我在第二个 for 循环之后添加了 {
字符.
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);
祝你好运.
这篇关于打印二维数组中的最大数字 - 为什么我的代码打印三个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:打印二维数组中的最大数字 - 为什么我的代码打印三个数字


基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01