[英]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);
}
}
split
返回一個大小為 12 的數組,其中每個元素都是輸入的數字之一。smallest
的 int 初始化為最大的int
,這保證了我與之比較的第一個int
保證更小。largest
的 . 我將它初始化為盡可能小的int
。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.