In: Computer Science
Ask the user to enter test scores. Once they have entered -1, print out the highest grade. Ensure that the grade entered is an integer.(Using Java Language)
Enter integers between 0 and 100, -1 when finished.
Enter a test score: [asdf]
Enter an integer.
Enter a test score: [32.5]
Enter an integer.
Enter a test score: [42]
Enter a test score: [99]
Enter a test score: [87]
Enter a test score: [x]
Enter an integer.
Enter a test score: [-1]
The max grade is: 99
import java.util.Scanner;
public class MaximumGrade {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int score, maxScore = Integer.MIN_VALUE;
while (true) {
System.out.print("Enter a test score: ");
try {
score = in.nextInt();
if (score > maxScore)
maxScore = score;
if (score == -1)
break;
} catch (Exception e) {
in.nextLine();
System.out.println("Enter an integer.");
}
}
System.out.println("The max grade is: " + maxScore);
}
}