vendredi 27 mars 2020

Why isn't the first value entered taken into account?

If the first number entered is the maximum or the minimum of all numbers entered, the program doesn't take that value as the minimum or maximum. What am I doing wrong?

    Scanner in = new Scanner(System.in);
    System.out.println("Please enter a set of integers: ");
    System.out.println("When done entering, enter a non integer (e.g. a floating-point number or string).");


    int smallest = in.nextInt();
    int largest = in.nextInt();

    while (in.hasNextDouble()) {
        int input = in.nextInt();
        if (input > largest) {
            largest = input;
        }
        else {  
        }
        if (input < smallest) { //doesn't include first input?
            smallest = input;
        }   
        else {  
        }
    }

    System.out.println("Maximum: " + largest);
    System.out.print("Minimum: " + smallest);
    in.close();
}

}

Aucun commentaire:

Enregistrer un commentaire