简体   繁体   English

使用 JOptionPane [关闭] 查找数组中的最高和最低数字

[英]find the highest and lowest number in array with JOptionPane [Closeed]

the code it's for education in college I need the user to insert a values of array and find which number is the highest and lowest but the lowest number doesn't work .用于大学教育的代码我需要用户插入数组的值并找到最高和最低但最低的数字不起作用。 can anyone explain this to me , sorry for asking much谁能给我解释一下,抱歉问得太多

int m=Integer.parseInt(JOptionPane.showInputDialog("Enter index"));       
int values;
int num [] = new int [m];

int max = num[0];
int min = num[0];
for (int i=0;i<num.length;i++)      
     {                          
     num[i] = values =Integer.parseInt(JOptionPane.showInputDialog("Enter value"));   
     System.out.println(num[i]);

     if (num[i] > max)
     {
         max=num[i];
     }
     if (num[i] < min )
     {
         min=num[i];
     }

}                                    
System.out.println("Largest Number in a given array is : " + max);
System.out.println("Smallest Number in a given array is : " + min);

Welcome,欢迎,

int[] num= new int[4];

After initialize num, num will be {0, 0, 0, 0};初始化num后,num将为{0, 0, 0, 0};

int max = num[0];
int min = num[0];

So now min will be 0, because num[0] is 0 - no matter what you enter.所以现在 min 将为 0,因为 num[0] 为 0 - 无论您输入什么。

num[i] = values =Integer.parseInt(JOptionPane.showInputDialog("Enter value"));   
System.out.println(num[i]);
if(i == 0)
      min = num[i];

Try this and think about it (same for max value)试试这个并考虑一下(最大值相同)

You can use Arrays.sort() to sort the array and simply access the first(0th) index which will hold the smallest number, and the last index which will hold the largest number:您可以使用Arrays.sort()对数组进行排序并简单地访问将保存最小数字的第一个(第 0 个)索引和将保存最大数字的最后一个索引:

int m=Integer.parseInt(JOptionPane.showInputDialog("Enter index"));       
 //int values; dont need this
 int num [] = new int [m]; //num is initialised with length entered by the user

 //int max = num[0]; no need
 //int min = num[0]; no need
 for (int i=0;i<num.length;i++)      
 {                          
  num[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter value"));   
  System.out.println(num[i]);      

 }
Arrays.sort(num); //this will sort your array in ascending order: smallest number will be at the 0th index and largest will be at the last index                                    
System.out.println("Largest Number in a given array is : " + num[num.length-1]);
System.out.println("Smallest Number in a given array is : " + num[0]);

There are a few errors in your code - so using your attempt, you can set the first value after it's been received from the user as the min and max, and work from there:您的代码中存在一些错误 - 因此,使用您的尝试,您可以从用户那里收到第一个值后将其设置为最小值和最大值,然后从那里开始工作:

int m = Integer.parseInt(JOptionPane.showInputDialog("Enter index"));
int num[] = new int[m];
int max = Integer.parseInt(JOptionPane.showInputDialog("Enter value"));
System.out.println(max);
int min = max;
for (int i = 0; i < num.length - 1; i++) {
    num[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter value"));
    System.out.println(num[i]);
    if (num[i] > max) {
        max = num[i];
    } else if (num[i] < min) {
        min = num[i];
    }
}
System.out.println("Largest Number in a given array is : " + max);
System.out.println("Smallest Number in a given array is : " + min);

With 5 inputs of 3, 8, 6, 4 and -33, the output is:具有 3、8、6、4 和 -33 的 5 个输入,输出为:

Largest Number in a given array is : 8给定数组中的最大数是:8

Smallest Number in a given array is : -33给定数组中的最小数是:-33


Just for fun, here's an alternative solution that's quite inefficient:只是为了好玩,这里有一个非常低效的替代解决方案:

System.out.println(Arrays.stream(num).boxed().mapToInt(Integer::intValue).max().getAsInt());
System.out.println(Arrays.stream(num).boxed().mapToInt(Integer::intValue).min().getAsInt());

Or of course you could sort the array with Arrays.sort() and then find the max with array[array.length() - 1] and the min with array[0]或者当然你可以用Arrays.sort()对数组进行排序,然后用array[array.length() - 1]找到最大值,用array[0]最小值

You can use the Integer.MAX_VALUE and Integer_MIN_VALUE .您可以使用Integer.MAX_VALUEInteger_MIN_VALUE max value for an integer is 2147483647(2^31-1) and min value for an integer is -2147483648(-2^31)整数的最大值为 2147483647(2^31-1),整数的最小值为 -2147483648(-2^31)

int m=Integer.parseInt(JOptionPane.showInputDialog("Enter index"));       
int num [] = new int [m];
int max = Integer.MAX_VALUE;
int min = Integer.MIN_VALUE;

 for (int i=0;i<num.length;i++)      
 {                          
     num[i] = values =Integer.parseInt(JOptionPane.showInputDialog("Enter value"));   
     System.out.println(num[i]);
     if (num[i] > max)
     {
         max=num[i];
     }
     if (num[i] < min )
     {
         min=num[i];
     }
}  

Hope this works for you.希望这对你有用。

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

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