Getting the lowest and highest value from integers without using arrays?

前端 未结 2 946
不知归路
不知归路 2020-12-12 06:13

I\'m trying to write a class which reads 5 integers from the user and returns the highest and lowest value back. This must be done using loops and without using arrays and I

2条回答
  •  时光取名叫无心
    2020-12-12 07:14

    here you go :)

    import java.util.Scanner;
    
        public class Ovning_321 {
            public static void main(String[] args){
                Scanner input = new Scanner(System.in);
                int number;
                int max = 0;
                int min = 0;
    
                      for (int x = 0; x<5; x++){ 
                            System.out.print("Give me an integer: "); 
                            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);
                }
         }
    

提交回复
热议问题