Print largest number in a 2d array - why do my code print three numbers

陌路散爱 提交于 2019-11-29 16:45:24

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.

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); needs to come out of the loop over the variable i. It's being printed 3 times because it's inside this loop.

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!