开发者

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

开发者 https://www.devze.com 2023-03-02 07:33 出处:网络
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?

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?

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.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号