Advertisement
Spocoman

02. Exam Preparation

Aug 29th, 2024 (edited)
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ExamPreparation {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int badGrades = Integer.parseInt(scanner.nextLine());
  7.         String task = "", lastProblem = "";
  8.         int badGradesCounter = 0, tasksCount = 0;
  9.         double sumGrades = 0, grade = 0;
  10.  
  11.         while (!(task = scanner.nextLine()).equals("Enough")) {
  12.             grade = Double.parseDouble(scanner.nextLine());
  13.             sumGrades += grade;
  14.             tasksCount++;
  15.  
  16.             if (grade <= 4) {
  17.                 badGradesCounter++;
  18.  
  19.                 if (badGradesCounter == badGrades) {
  20.                     System.out.printf("You need a break, %d poor grades.\n", badGrades);
  21.                     break;
  22.                 }
  23.             }
  24.  
  25.             lastProblem = task;
  26.         }
  27.  
  28.         if (task.equals("Enough")) {
  29.             System.out.printf("Average score: %.2f\n", sumGrades / tasksCount);
  30.             System.out.printf("Number of problems: %d\n", tasksCount);
  31.             System.out.printf("Last problem: %s\n", lastProblem);
  32.         }
  33.     }
  34. }
  35.  
  36. ИЛИ:
  37.  
  38. import java.util.Scanner;
  39.  
  40. public class ExamPreparation {
  41.     public static void main(String[] args) {
  42.         Scanner scanner = new Scanner(System.in);
  43.         int badGrades = Integer.parseInt(scanner.nextLine());
  44.         String task = "", lastProblem = "";
  45.         int badGradesCounter = 0, tasksCount = 0;
  46.         double sumGrades = 0, grade = 0;
  47.  
  48.         while (badGradesCounter != badGrades
  49.                 &&!(task = scanner.nextLine()).equals("Enough")) {
  50.             grade = Double.parseDouble(scanner.nextLine());
  51.             if (grade <= 4) {
  52.                 badGradesCounter++;
  53.             }
  54.             sumGrades += grade;
  55.             lastProblem = task;
  56.             tasksCount++;
  57.         }
  58.  
  59.         if (badGradesCounter == badGrades) {
  60.             System.out.printf("You need a break, %d poor grades.\n", badGrades);
  61.         } else {
  62.             System.out.printf("Average score: %.2f\n", sumGrades / tasksCount);
  63.             System.out.printf("Number of problems: %d\n", tasksCount);
  64.             System.out.printf("Last problem: %s\n", lastProblem);
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement