Java using loop, accept five integers, input from console and return the min & max

398 Views Asked by At

I am trying to create a Java code using a loop that will accept five integers input from console and return the smallest and the largest. I am having a problem with the code coming together.Thank you

import java.util.*;
public class Highest_LowestofFiveIntegers {

public static void main(String[] args) {

    System.out.print("Enter five integers separated by a 
          space i.e. 2 6 55 1 3 910: "); 
    int number = 0;

    number = input.nextInt(); 
    int max = 0;
    int min = 0;

    for (int x = 0; x<5; x++){ 


        if (x == 0 || number > max){ 
        max = number;  
        }               
        if (x == 0 || number < min){ 
        min = number;  
        }               
     }                 
         System.out.println("Highest value: " + max);
         System.out.println("Lowest value: " + min);

}

}

1

There are 1 best solutions below

5
On

Put number = input.nextInt(); in the for loop; not before it.

int max = 0, min = 0;
for (int x = 0; x < 5; x++) { 
    int number = input.nextInt();
    if (x == 0 || number > max){ 
        max = number;  
    }               
    if (x == 0 || number < min){ 
        min = number;  
    }               
}   
System.out.println("Highest value: " + max);
System.out.println("Lowest value: " + min);