Advertisement
GabrielHr00

2. Exam Preparation

Feb 2nd, 2025
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. package whileLoop_Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ExamPreparation {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int badGradesLimit = Integer.parseInt(scanner.nextLine());
  9.  
  10.         int gradesSum = 0;
  11.         int gradesCount = 0;
  12.         int badGradesCount = 0;
  13.         String lastProblemName = "";
  14.  
  15.         String command = scanner.nextLine();
  16.         while (!command.equals("Enough")) {
  17.             String taskName = command;
  18.             int grade = Integer.parseInt(scanner.nextLine());
  19.  
  20.             if (grade <= 4) {
  21.                 badGradesCount++;
  22.  
  23.                 if (badGradesCount == badGradesLimit) {
  24.                     break;
  25.                 }
  26.             }
  27.  
  28.             lastProblemName = taskName;
  29.             gradesCount++;
  30.             gradesSum += grade;
  31.  
  32.             command = scanner.nextLine();
  33.         }
  34.  
  35.         if (badGradesLimit == badGradesCount) {
  36.             System.out.printf("You need a break, %d poor grades.", badGradesLimit);
  37.         }
  38.         else {
  39.             System.out.printf("Average score: %.2f%n" +
  40.                     "Number of problems: %d%n" +
  41.                     "Last problem: %s", 1.0 * gradesSum / gradesCount, gradesCount, lastProblemName);
  42.         }
  43.     }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement