desislava_topuzakova

Untitled

Jul 6th, 2021
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. package MidExam;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class HeartDelivery {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. String[] input = scanner.nextLine().split("@");
  11. List<Integer> neighborhood = Arrays.stream(input).map(Integer::parseInt).collect(Collectors.toList());
  12.  
  13. String command = scanner.nextLine();
  14.  
  15. int indexNow = 0;
  16.  
  17. while (!"Love!".equals(command)) {
  18. String[] tokens = command.split("\\s+");
  19. int jumpLength = Integer.parseInt(tokens[1]);
  20. indexNow += jumpLength;
  21.  
  22. if (indexNow >= neighborhood.size()) {
  23. indexNow = 0;
  24. }
  25.  
  26. if (equalZero(neighborhood.get(indexNow))) {
  27. System.out.println(String.format("Place %d already had Valentine's day.", indexNow));
  28. command = scanner.nextLine();
  29. continue;
  30. }
  31.  
  32. neighborhood.set(indexNow, neighborhood.get(indexNow) - 2);
  33.  
  34. if (equalZero(neighborhood.get(indexNow))) {
  35. System.out.println(String.format("Place %d has Valentine's day.", indexNow));
  36. }
  37.  
  38. command = scanner.nextLine();
  39. }
  40.  
  41. System.out.println(String.format("Cupid's last position was %s.", indexNow));
  42.  
  43. int count = 0;
  44.  
  45. for (Integer integer : neighborhood) {
  46. if (!(integer == 0)) {
  47. count++;
  48. }
  49. }
  50.  
  51. if (count != 0) {
  52. System.out.println(String.format("Cupid has failed %d places.", count));
  53. } else {
  54. System.out.println("Mission was successful.");
  55. }
  56. }
  57.  
  58. private static boolean equalZero(int indexNow) {
  59. return indexNow == 0;
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment