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

后端 未结 8 1031
终归单人心
终归单人心 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:46

    here you need to skip int 0 like following:

    val = s.nextInt();
      if ((val < min) && (val!=0)) {
          min = val;
      }
    
    0 讨论(0)
  • 2020-11-28 16:50
    System.out.print("Enter a Value: ");
    val = s.nextInt();
    

    This line is placed in last.The whole code is as follows:-

    public static void main(String[] args){
        int min, max;
    
        Scanner s = new Scanner(System.in);
        System.out.print("Enter a Value: ");
        int val = s.nextInt();
        min = max = val;
    
        while (val != 0) {
            if (val < min) {
                min = val;
            }
            if (val > max) {
                max = val;
            }
            System.out.print("Enter a Value: ");
            val = s.nextInt();
    
        }
        System.out.println("Min: " + min);
        System.out.println("Max: " + max);
    }
    
    0 讨论(0)
提交回复
热议问题