Advertisement
desislava_topuzakova

3. Heart Delivery (Array)

Jun 14th, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. package ExamPreparation;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class ThirdTask {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. //входни данни
  11. int [] houses = Arrays.stream(scanner.nextLine() //"10@10@10@2"
  12. .split("@")) //["10", "10", "10", "2"]
  13. .mapToInt(Integer::parseInt) //[10, 10, 10, 2]
  14. .toArray();
  15.  
  16. int currentIndex = 0; //на коя къща се намира Купидон
  17.  
  18. String command = scanner.nextLine();
  19.  
  20. while (!command.equals("Love!")) {
  21. //command = "Jump 3".split(" ") -> ["Jump", "3"]
  22. int jumpLength = Integer.parseInt(command.split(" ")[1]);
  23. //скача
  24. currentIndex += jumpLength;
  25.  
  26. //проверка дали е извън квартала
  27. if (currentIndex > houses.length - 1) {
  28. currentIndex = 0;
  29. }
  30.  
  31. //къщата, в която отива -> проверка има ли нужда -> дава сърца
  32. if (houses[currentIndex] != 0) { //имаме нужда от сърца, защото бр. нужни сърца != 0
  33. houses[currentIndex] -= 2;
  34. //ако след даването вече нямаме нужда
  35. if (houses[currentIndex] == 0) {
  36. System.out.printf("Place %d has Valentine's day.%n", currentIndex);
  37. }
  38. } else {
  39. //къщата, в която отива -> брой нужни сърца = 0
  40. System.out.printf("Place %d already had Valentine's day.%n", currentIndex);
  41. }
  42. command = scanner.nextLine();
  43. }
  44.  
  45. //къща = 0 -> няма нужда да и даваме -> може да празнува празника
  46. //къща != 0 -> има нужда от даване на сърца
  47. System.out.printf("Cupid's last position was %d.%n", currentIndex);
  48. //успешна мисия -> всички стойности на къщите да са 0
  49. boolean isFailed = false;
  50. //isFailed = true -> провал
  51. //isFailed = false -> успешна
  52. int countFailedHouse = 0;
  53. for (int house : houses) {
  54. if (house != 0) {
  55. isFailed = true;
  56. countFailedHouse++;
  57. }
  58. }
  59. //isFailed = false -> преминали през всички къщи = 0
  60. if (isFailed) {
  61. System.out.printf("Cupid has failed %d places.%n", countFailedHouse);
  62. } else {
  63. //isFailed = false
  64. System.out.println("Mission was successful.");
  65. }
  66. }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement