Answer:
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- int pass = 0;
- int fail = 0;
- int highest = 0;
- int lowest = 100;
- int counter = 0;
-
- Scanner input = new Scanner(System.in);
-
- while(counter < 5){
- System.out.print("Input a score between 0 to 100: ");
- int score = input.nextInt();
- while(score < 0 || score > 100){
- System.out.println("Invalid score.");
- System.out.print("Input a score between 0 to 100: ");
- score = input.nextInt();
- }
-
- if(score >= 60 ){
- System.out.println("Pass");
- pass++;
- }else{
- System.out.println("Fail");
- fail++;
- }
-
- if(highest < score ){
- highest = score;
- }
-
- if(lowest > score){
- lowest = score;
- }
-
- counter++;
- }
-
- System.out.println("Total number of passes is: " + pass);
- System.out.println("Total number of failures is: " + fail);
- System.out.println("Highest score is: " + highest);
- System.out.println("Lowest score is: " + lowest);
- }
- }
Step-by-step explanation:
Firstly, declare the necessary variables and initialize them with zero (Line 4-8). Next create a Scanner object to get user input for score (Line 10). Create a while loop by using the counter as limit (Line 12). In the while loop, prompt user to input a number between 1 - 100 (Line 13-14). Create another while loop to check the input must be between 1 - 100 or it will print invalid message and ask for user input again (Line 15-19).
Next, create an if-else statement to check if the current score is equal or above 60. If so print pass if not print fail (Line 21-27). At the same time increment the fail and pass counter.
Create another two if statement to get the current highest and lowest score and assign them to highest and lowest variables, respectively (Line 29-35).
Increment the counter by one before proceeding to the next loop to repeat the same process (Line 37).
At last, print the required output after finishing the while loop (Line 40-43).