繁体   English   中英

如何找到二维数组中的最低和最高数字?

[英]How can I find the lowest and highest number in 2d array?

package HighLow;

import javax.swing.JOptionPane;

public class HighLow {
public static void main (String[]args) {
int [][] arr= new int[3][4];
int smallest=arr[0][0];
int largest= arr[0][0];
int i = 0;
int j = 0;
{
arr[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Please enter 12 numbers: ")); 
{
if (arr[i][j] < smallest) {
smallest = arr[i][j];
}
if (arr[0][0] > largest) {
largest = arr[i][j];
}
{
JOptionPane.showMessageDialog(null, "The smallest value in the Array is:" + smallest);
JOptionPane.showMessageDialog(null, "The largest value in the Array is:" + largest);        
}

}

}}}

如何找到二维数组中的最低和最高数字? 我的代码出错了? 似乎有什么问题? 我只需要在用户输入的 12 个数字中找到最高和最低的。 这是我到目前为止所拥有的。

如何找到二维数组中的最低和最高数字? 我的代码出错了? 似乎有什么问题? 我只需要在用户输入的 12 个数字中找到最高和最低的。 这是我到目前为止所拥有的。

您没有循环接收您的阵列,因此收到错误。 要接受数组的输入,请使用此

for (int i = 0;i<3;i++)
    for (int j = 0;j<4;j++)
        arr[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Please enter 12 numbers: ")); 

因此,您将让该框输出 12 次以供用户输入 12 个整数,然后一旦您拥有数组,只需运行一个简单的排序即可获得最小和最大。

int smallest  = arr[0][0];
int largest = arr[0][0];

for (int i = 0;i<3;i++)
    for (int j = 0;j<4;j++) {
        if (arr[i][j]<smallest)
            smallest = arr[i][j];
        else if(arr[i][j]>largest)
            largest = arr[i][j];
    }
}

最终代码是

public class Solution {
    public static void main (String[]args) {
        int [][] arr= new int[3][4];

        for (int i = 0;i<3;i++)
            for (int j = 0;j<4;j++)
                arr[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Please enter 12 numbers: ")); 

        int smallest  = arr[0][0];
        int largest = arr[0][0];

        for (int i = 0;i<3;i++)
            for (int j = 0;j<4;j++) {
                if (arr[i][j]<smallest)
                    smallest = arr[i][j];
                else if(arr[i][j]>largest)
                    largest = arr[i][j];
            }
        }

        JOptionPane.showMessageDialog(null, "The smallest value in the Array is:" + smallest);
        JOptionPane.showMessageDialog(null, "The largest value in the Array is:" + largest);        
    }

}

错误主要出现在初始化部分,您假设第一行和第一列中的元素是数组中的最小或最大元素。

您必须使用一个肯定高于数组中所有值的值来初始化数组的最小值,为什么? 因为只有这样才会更新数组的实际最小值,例如,如果您将最小值设置为 1000,那么如果数组的实际最小值为 300,它将被更新。 但是,如果您将最小值设置为例如 30,那么 300(即实际最小值)将不会被记录,并且会出现错误。

绝对相反适用于数组最大值的初始化。 在初始化时,安全的球场估计通常会起作用,因此请相应地进行初始化。

就个人而言,我不认为您应该使用JOptionPane从用户那里获取数据并在 Java 控制台应用程序(例如您的)中向用户显示数据。 我更喜欢使用Scanner

(以下代码演示。代码后注释。)

请注意,调用JOptionPane的方法将启动事件调度线程(EDT),这意味着您的应用程序不会终止 - 除非您添加System.exit(0); .

package HighLow;

public class HighLow {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        System.out.print("Please enter 12 numbers: ");
        String line = stdin.nextLine();
        String[] numbers = line.split(" ");
        int k = 0;
        int[][] arr = new int[3][4];
        int smallest = Integer.MAX_VALUE;
        int largest = Integer.MIN_VALUE;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 4; j++) {
                arr[i][j] = Integer.parseInt(numbers[k++]);
                if (arr[i][j] < smallest) {
                    smallest = arr[i][j];
                }
                if (arr[i][j] > largest) {
                    largest = arr[i][j];
                }
            }
        }
        System.out.println("The smallest value in the Array is: " + smallest);
        System.out.println("The largest value in the Array is: " + largest);
    }
}
  1. 我假设用户在一行中输入了十二个由空格分隔的数字。
  2. 方法split返回一个大小为 12 的数组,其中每个元素都是输入的数字之一。
  3. 我将smallest的 int 初始化为最大的int ,这保证了我与之比较的第一个int保证更小。
  4. 对于largest的 . 我将它初始化为尽可能小的int
  5. 您需要一个循环来填充arr的所有元素。

这是来自上述代码的示例运行的 output。

Please enter 12 numbers: 1 2 3 4 5 6 7 8 9 0 -1 -2
The smallest value in the Array is: -2
The largest value in the Array is: 9

编辑

由于您声称需要JOptionPane ,因此下面的代码使用JOptionPane

String line = JOptionPane.showInputDialog("Please enter 12 numbers: ");
String[] numbers = line.split(" ");
int k = 0;
int[][] arr = new int[3][4];
int smallest = Integer.MAX_VALUE;
int largest = Integer.MIN_VALUE;
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
        arr[i][j] = Integer.parseInt(numbers[k++]);
        if (arr[i][j] < smallest) {
            smallest = arr[i][j];
        }
        if (arr[i][j] > largest) {
            largest = arr[i][j];
        }
    }
}
JOptionPane.showMessageDialog(null, "The smallest value in the Array is:" + smallest);
JOptionPane.showMessageDialog(null, "The largest value in the Array is:" + largest);
System.exit(0);

暂无
暂无

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

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