How do I get the max and min values from a set of numbers entered?

后端 未结 8 1032
终归单人心
终归单人心 2020-11-28 15:52

Below is what I have so far:

I don\'t know how to exclude 0 as a min number though. The assignment asks for 0 to be the exit number so I need to have the lowest numb

8条回答
  •  再見小時候
    2020-11-28 16:44

    Here's a possible solution:

    public class NumInput {
      public static void main(String [] args) {
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
    
        Scanner s = new Scanner(System.in);
        while (true) {
          System.out.print("Enter a Value: ");
          int val = s.nextInt();
    
          if (val == 0) {
              break;
          }
          if (val < min) {
              min = val;
          }
          if (val > max) {
             max = val;
          }
        }
    
        System.out.println("min: " + min);
        System.out.println("max: " + max);
      }
    }
    

    (not sure about using int or double thought)

提交回复
热议问题