151k views
3 votes
Implement a Java program using simple console input & output and logical control structures such that the program prompts the user to enter 5 integer scores between 0 to 100 as inputs and does the following tasks For each input score, the program determines whether the corresponding score maps to a pass or a fail. If the input score is greater than or equal to 60, then it should print "Pass", otherwise, it should print "Fail". After the 5 integer scores have been entered, the program counts the total number of passes as well as the total number of failures and prints those 2 count values with appropriate messages like "Total number of passes is: " & "Total number of failures is: ". After the 5 integer scores have been entered, the program finds the highest score as well as the lowest score and prints those 2 values with appropriate messages like "Highest score is: " & "Lowest score is: ". The program checks whether an input score is a number between 0 - 100 or not. If the input score value is otherwise or outside the above range, then it prints the error message saying "Invalid score" and prompts the user for valid input.

User Pablomarti
by
5.4k points

1 Answer

1 vote

Answer:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. int pass = 0;
  5. int fail = 0;
  6. int highest = 0;
  7. int lowest = 100;
  8. int counter = 0;
  9. Scanner input = new Scanner(System.in);
  10. while(counter < 5){
  11. System.out.print("Input a score between 0 to 100: ");
  12. int score = input.nextInt();
  13. while(score < 0 || score > 100){
  14. System.out.println("Invalid score.");
  15. System.out.print("Input a score between 0 to 100: ");
  16. score = input.nextInt();
  17. }
  18. if(score >= 60 ){
  19. System.out.println("Pass");
  20. pass++;
  21. }else{
  22. System.out.println("Fail");
  23. fail++;
  24. }
  25. if(highest < score ){
  26. highest = score;
  27. }
  28. if(lowest > score){
  29. lowest = score;
  30. }
  31. counter++;
  32. }
  33. System.out.println("Total number of passes is: " + pass);
  34. System.out.println("Total number of failures is: " + fail);
  35. System.out.println("Highest score is: " + highest);
  36. System.out.println("Lowest score is: " + lowest);
  37. }
  38. }

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).

User Leandrojmp
by
4.9k points