简体   繁体   English

打印二维数组中的最大数字 - 为什么我的代码打印三个数字

[英]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.我的问题是我的 output 是三个数字而不是一个 - 最大的。 Why?为什么?

Here is my code:这是我的代码:

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("\nMax 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).直到最后一个指令序列之前的所有内容都是正确的(尽管格式不正确)。

Here is original:这里是原文:

int maxValue = 1;
System.out.println("\nMax 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("\nMax 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.仔细看,你会发现我在第二个 for 循环之后添加了{字符。

If you wanted to find total max, and minimize open and close curly-braces here is another version:如果你想找到总最大值,并最小化打开和关闭花括号,这里是另一个版本:

int maxValue = 0;

System.out.println("\nMax 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.祝你好运。

    int m,n,max;
    int a[][]=new int[10][10];
    Scanner S=new Scanner(System.in);
    System.out.println("Enter m*n matrix");
    m=S.nextInt();
    n=S.nextInt();
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            a[i][j]=S.nextInt();
        }
    }
    max=a[0][0];
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(a[i][j]>max)
            {
                max=a[i][j];
            }
        }
    }
    System.out.println(max);

Your line System.out.println(maxValue);你的线System.out.println(maxValue); needs to come out of the loop over the variable i .需要退出变量i的循环。 It's being printed 3 times because it's inside this loop.它被打印了 3 次,因为它在这个循环中。

This would be easier to see if your code was indented properly;这将更容易查看您的代码是否正确缩进; this is a good habit to get into anyway.无论如何,这是一个好习惯。

The answer is in your code once it's indented correctly:一旦正确缩进,答案就在您的代码中:

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);
    }
}

Don't underestimate how useful good indentation can be for catching this kind of bug:)不要低估良好的缩进对于捕捉这种错误的作用:)

    int max;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of rows : ");
    int n = sc.nextInt();
    System.out.println("Enter number of columns : ");
    int m = sc.nextInt();
    int[][] array = new int[n][m];
    System.out.println("Enter the elements of array : ");
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            System.out.print("X[" + i + "," + j + "]" + "=");
            array[i][j] = sc.nextInt();
        }
    }
    max = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (array[i][j] > max) {
                max = array[i][j];
            }
        }
    }
    System.out.println("Max value of the array is " + max);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM