简体   繁体   中英

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

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

So now min will be 0, because num[0] is 0 - no matter what you enter.

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:

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:

Largest Number in a given array is : 8

Smallest Number in a given array is : -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]

You can use the Integer.MAX_VALUE and Integer_MIN_VALUE . max value for an integer is 2147483647(2^31-1) and min value for an integer is -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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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