Advertisement
Guest User

Tickets

a guest
Aug 19th, 2020
903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TicketsRepair {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. // Броячите - от тип int
  8. int studentCount = 0;
  9. int standardCount = 0;
  10. int kidCount = 0;
  11.  
  12. int counterTotal = 0;
  13.  
  14. String movieName = scanner.nextLine();
  15. while (!(movieName.equals("Finish"))) {
  16. int freeSpaces = Integer.parseInt(scanner.nextLine());
  17. // Нулираме при всеки филм
  18. counterTotal = 0;
  19.  
  20. // Използвайки for, вече нямаме нужда от counter
  21. // Завъртаме докато има свободни места или в случай на команда End
  22. for (int i = 0; i < freeSpaces; i++) {
  23. String type = scanner.nextLine();
  24. if (type.equals("End")) {
  25. break;
  26. }
  27.  
  28. counterTotal++;
  29. switch (type) {
  30. case "student":
  31. studentCount++;
  32. break;
  33. case "standard":
  34. standardCount++;
  35. break;
  36. default:
  37. kidCount++;
  38. break;
  39. }
  40. }
  41.  
  42. // Променливата може да бъде създадена направо тук
  43. double hall = (double) counterTotal / freeSpaces * 100;
  44. System.out.printf("%s - %.2f%% full.%n", movieName, hall);
  45.  
  46. movieName = scanner.nextLine();
  47. }
  48.  
  49. counterTotal = studentCount + standardCount + kidCount;
  50. System.out.printf("Total tickets: %d%n", counterTotal);
  51.  
  52. // Променливите, изчисляващи процентите се използват само в края на тази програма,
  53. // затова можем да ги създадем директно преди да ги използваме
  54. double studentPercent = (100.0 / counterTotal ) * studentCount;
  55. System.out.printf("%.2f%% student tickets.%n", studentPercent);
  56.  
  57. double standardPercent = (100.0 / counterTotal) * standardCount;
  58. System.out.printf("%.2f%% standard tickets.%n", standardPercent);
  59.  
  60. double kidPercent = (100.0 / counterTotal) * kidCount;
  61. System.out.printf("%.2f%% kids tickets.", kidPercent);
  62.  
  63. }
  64. }
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement