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

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

    You just need to keep track of a max value like this:

    int maxValue = 0;
    

    Then as you iterate through the numbers, keep setting the maxValue to the next value if it is greater than the maxValue:

    if (value > maxValue) {
        maxValue = value;
    }
    

    Repeat in the opposite direction for minValue.

    0 讨论(0)
  • 2020-11-28 16:34

    It is better

    public class Main {
    
        public static void main(String[] args) {
    
            System.out.print("Enter numbers: ");
            Scanner input = new Scanner(System.in);
    
            double max = Double.MIN_VALUE;
            double min = Double.MAX_VALUE;
    
            while (true) {
    
                if ( !input.hasNextDouble())
                    break;
    
                Double num = input.nextDouble();
    
                min = Math.min(min, num);
                max = Math.max(max, num);
    
            }
            System.out.println("Max is: " + max);
            System.out.println("Min is: " + min);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 16:40

    I tried to optimize solution by handling user input exceptions.

    public class Solution {
        private static Integer TERMINATION_VALUE = 0;
        public static void main(String[] args) {
            Integer value = null;
            Integer minimum = Integer.MAX_VALUE;
            Integer maximum = Integer.MIN_VALUE;
            Scanner scanner = new Scanner(System.in);
            while (value != TERMINATION_VALUE) {
                Boolean inputValid = Boolean.TRUE;
                try {
                    System.out.print("Enter a value: ");
                    value = scanner.nextInt();
                } catch (InputMismatchException e) {
                    System.out.println("Value must be greater or equal to " + Integer.MIN_VALUE + " and less or equals to " + Integer.MAX_VALUE );
                    inputValid = Boolean.FALSE;
                    scanner.next();
                }
                if(Boolean.TRUE.equals(inputValid)){
                    minimum = Math.min(minimum, value);
                    maximum = Math.max(maximum, value);
                }
            }
            if(TERMINATION_VALUE.equals(minimum) || TERMINATION_VALUE.equals(maximum)){
                System.out.println("There is not any valid input.");
            } else{
                System.out.println("Minimum: " + minimum);
                System.out.println("Maximum: " + maximum);
            }
            scanner.close();
        }
    }
    
    
    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2020-11-28 16:46
    //for excluding zero
    public class SmallestInt {
    
        public static void main(String[] args) {
    
            Scanner input= new Scanner(System.in);
    
            System.out.println("enter number");
            int val=input.nextInt();
            int min=val;
    
            //String notNull;
    
            while(input.hasNextInt()==true)
            {
                val=input.nextInt();
                if(val<min)
                    min=val;
            }
            System.out.println("min is: "+min);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 16:46

    This is what I did and it works try and play around with it. It calculates total,avarage,minimum and maximum.

    public static void main(String[] args) {
       int[] score= {56,90,89,99,59,67};
       double avg;
       int sum=0;
       int maxValue=0;
       int minValue=100;
    
       for(int i=0;i<6;i++){
           sum=sum+score[i];
       if(score[i]<minValue){
        minValue=score[i];
       }
       if(score[i]>maxValue){
        maxValue=score[i];
       }
       }
       avg=sum/6.0;
    System.out.print("Max: "+maxValue+"," +" Min: "+minValue+","+" Avarage: "+avg+","+" Sum: "+sum);}
    
     }
    
    0 讨论(0)
提交回复
热议问题