desislava_topuzakova

04. Easter Eggs Battle

Feb 19th, 2022
1,245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class EasterEggBattle_05 {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int firstPlayerEggsCount = Integer.parseInt(scanner.nextLine()); //бр. яйца на първия
  7.         int secondPlayerEggsCount = Integer.parseInt(scanner.nextLine()); //бр. яйца на втория
  8.  
  9.         //повтаряме: въвеждаме команда
  10.         //спираме: команда == "End of battle"
  11.         //продължаваме: команда != "End of battle"
  12.         String command = scanner.nextLine(); // "End of battle" или "one" / "two"
  13.  
  14.         while (!command.equals("End of battle")) {
  15.             //команда -> "one" или "two"
  16.             //битка
  17.             if (command.equals("one")) {
  18.                 secondPlayerEggsCount--;
  19.             } else if (command.equals("two")) {
  20.                 firstPlayerEggsCount--;
  21.             }
  22.             //проверка имаме ли победител
  23.             if (firstPlayerEggsCount == 0) {
  24.                 System.out.printf("Player one is out of eggs. Player two has %d eggs left.", secondPlayerEggsCount);
  25.                 break;
  26.             } else if (secondPlayerEggsCount == 0) {
  27.                 System.out.printf("Player two is out of eggs. Player one has %d eggs left.", firstPlayerEggsCount);
  28.                 break;
  29.             }
  30.  
  31.             command = scanner.nextLine();
  32.         }
  33.  
  34.         if (command.equals("End of battle")) {
  35.             System.out.printf("Player one has %d eggs left.%n", firstPlayerEggsCount);
  36.             System.out.printf("Player two has %d eggs left.", secondPlayerEggsCount);
  37.         }
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment