Search code examples
javado-while

How to get min/max from user input?


In my cs class we're required to find highest and lowest numbers that a user has inputted between -100 and 100, utilizing a do-while loop. I am having trouble with this because it seems like the values are just using the initialized values instead of the user inputted value. The professor tells me to just figure it out and I can't.

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double number;
        double max = 0;
        double min = 0;
        
        
        // Do while loop that asks the user for values from -100, 100, and determine max and min
        do {
            System.out.print("Enter value:");
            number = input.nextDouble();
            if(number == -19.5) {
                break;
            }
            
            if (number >= -100 && number <= 100) {
                
                if (number > max) {
                max = number;
                }
                if (number < min) {
                min = number;
                }
                    
            } else {
                System.out.print("There were no valid values");
            }
            
        } while (true);
                    
        System.out.println("The max value was " + max);
        System.out.println("The min value was " + min);
        
    }
}

I have tried initializing the variable to the Integer.MAX/MIN values but it still prints the initialized value. I know it is something to do with my logic but I just cannot see the error.


Solution

  • Seems setting initial max and min. When initializing both to 0, program will cause an error when the first user input is negative, as any negative number will be less than 0, and it will update both max and min with the first user input.

    Try setting Double.MAX_VALUE to min and max to Double.MIN_VALUE so that any valid input will update them correctly. Refer the code sample below :

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            double number;
            double max = Double.MIN_VALUE;  // Init MAXIMUM to the SMALLEST possible double value
            double min = Double.MAX_VALUE;  // Init MINIMUM to the LARGEST possible double value
    
           
            do {
                System.out.print("Enter value: ");
                number = input.nextDouble();
    
                if (number == -19.5) {
                    break;
                }
    
                if (number >= -100 && number <= 100) {
                    if (number > max) {
                        max = number;
                    }
                    if (number < min) {
                        min = number;
                    }
                } else {
                    System.out.println("Invalid value. Enter a value between -100 and 100.");
                }
    
            } while (true);
    
            System.out.println("The max value was " + max);
            System.out.println("The min value was " + min);
        }
    }
    

    Refer the working code sample here