Advertisement
GabrielHr00

ExamPreparation

Nov 27th, 2022
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. package S5_WhileLoops;
  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 badGradesCount = 0;
  11. String lastProblem = "";
  12. int tasksCount = 0;
  13. int gradesSum = 0;
  14.  
  15. // read command
  16. String command = scanner.nextLine();
  17.  
  18. while (!command.equals("Enough")) {
  19. String taskName = command;
  20. int grade = Integer.parseInt(scanner.nextLine());
  21.  
  22. // increment for printing
  23. gradesSum = gradesSum + grade;
  24. tasksCount++;
  25. lastProblem = taskName;
  26.  
  27. // check for bad grades
  28. if(grade <= 4) {
  29. badGradesCount++;
  30. }
  31.  
  32. // check if we have enough bad grades to stop
  33. if(badGradesCount == badGradesLimit) {
  34. break;
  35. }
  36.  
  37. // read command once again
  38. command = scanner.nextLine();
  39. }
  40.  
  41.  
  42. // check if we got too many bad grades
  43. if(badGradesCount == badGradesLimit) {
  44. System.out.printf("You need a break, %d poor grades.",badGradesCount);
  45. } else {
  46. System.out.printf("Average score: %.2f%n", 1.0*gradesSum/tasksCount);
  47. System.out.printf("Number of problems: %d%n", tasksCount);
  48. System.out.printf("Last problem: %s", lastProblem);
  49. }
  50.  
  51. }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement