二维数组java中的最小值和最大值

时间:2022-09-28 14:15:42

I want to output max and min value of 2d array. Max works well, but min always outputs zero even when theres no zeros in array.I set Math.random() to 99 to prevent smaller chance to get zero in array for this example. Heres full code:

我想输出二维数组的最大值和最小值。Max工作得很好,但是min总是输出0,即使数组中没有0。我将Math.random()设置为99,以防止在本例的数组中获得0的可能性更小。这是完整的代码:

public class e {

public static void main(String[] args)   {

    int a[][] = new int [5][5];
    int l = a[0][0];
    int m = a[0][0];
    int i,j,r,k;


    for(i=0;i<a.length;i++)                   // 
        for(j=0;j<a[i].length;j++){           // 2d array random number generator
            a[i][j] =(int)(Math.random()*99); //
         }
    for(i=0;i<a.length;i++){               //
        for(j=0;j<a[i].length;j++)         //
                                           // create 2d array and output it
    System.out.print(a[i][j] + "\t");      //   
    System.out.println();                  //

}
    System.out.println("\t"); 
        for(r=0;r<a.length;r++){           //
            for(k=0;k<a.length;k++)        //
                if(a[r][k] < m){           // finds a min value
                    m = a[r][k];           //

            }
        }

    System.out.println("\t");               // 
        for(i=0;i<a.length;i++){            //
            for(j=0;j<a.length;j++)         // finds a max value
                if(a[i][j] > l){            //
                    l = a[i][j];            //

            }
        }
    System.out.println("min value is " + m); //outputs min value
    System.out.println("max value is " + l); // outputs max value
            }
       }

1 个解决方案

#1


4  

Because of the way you choose the random values in a, there will be no value less than zero - but there is also no guarantee that any of the values will be exactly zero. However, you initialize m to be zero, since that is the default value of the array elements; nothing can be smaller than this, so the answer is always zero.

由于你选择a中的随机值的方式,不存在小于零的值——但是也不能保证所有的值都是零。但是,将m初始化为0,因为这是数组元素的默认值;没有比这个小的,所以答案总是0。

You should initialize your m = a[0][0] immediately before you start the outer for loop in the block labelled "finds a min value", i.e.

您应该在启动名为“查找最小值”的块中的外部for循环之前,立即初始化您的m = a[0][0],即。

    m = a[0][0];
    for(r=0;r<a.length;r++){           //
        for(k=0;k<a.length;k++)        //
            if(a[r][k] < m){           // finds a min value
                m = a[r][k];           //

        }
    }

Alternatively, you can set m = Integer.MAX_VALUE (and l = Integer.MIN_VALUE), since these are guaranteed to have values smaller and larger than them, respectively.

或者,您可以设置m = Integer。MAX_VALUE(和l = Integer.MIN_VALUE),因为它们的值必须分别小于和大于它们。

#1


4  

Because of the way you choose the random values in a, there will be no value less than zero - but there is also no guarantee that any of the values will be exactly zero. However, you initialize m to be zero, since that is the default value of the array elements; nothing can be smaller than this, so the answer is always zero.

由于你选择a中的随机值的方式,不存在小于零的值——但是也不能保证所有的值都是零。但是,将m初始化为0,因为这是数组元素的默认值;没有比这个小的,所以答案总是0。

You should initialize your m = a[0][0] immediately before you start the outer for loop in the block labelled "finds a min value", i.e.

您应该在启动名为“查找最小值”的块中的外部for循环之前,立即初始化您的m = a[0][0],即。

    m = a[0][0];
    for(r=0;r<a.length;r++){           //
        for(k=0;k<a.length;k++)        //
            if(a[r][k] < m){           // finds a min value
                m = a[r][k];           //

        }
    }

Alternatively, you can set m = Integer.MAX_VALUE (and l = Integer.MIN_VALUE), since these are guaranteed to have values smaller and larger than them, respectively.

或者,您可以设置m = Integer。MAX_VALUE(和l = Integer.MIN_VALUE),因为它们的值必须分别小于和大于它们。