Advertisement
Spocoman

Easter Eggs Battle

Sep 4th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int firstPlayer = Integer.parseInt(scanner.nextLine()),
  7.                 secondPlayer = Integer.parseInt(scanner.nextLine());
  8.         String command;
  9.         while (true) {
  10.             command = scanner.nextLine();
  11.             if (command.equals("End")) {
  12.                 System.out.println("Player one has " + firstPlayer + " eggs left.\n"
  13.                         + "Player two has " + secondPlayer + " eggs left.");
  14.                 break;
  15.             } else if (command.equals("one")) {
  16.                 secondPlayer--;
  17.                 if (secondPlayer == 0) {
  18.                     System.out.println("Player two is out of eggs. Player one has " + firstPlayer + " eggs left.");
  19.                     break;
  20.                 }
  21.             } else if (command.equals("two")) {
  22.                 firstPlayer--;
  23.                 if (firstPlayer == 0) {
  24.                     System.out.println("Player one is out of eggs. Player two has " + secondPlayer + " eggs left.");
  25.                     break;
  26.                 }
  27.             }
  28.         }
  29.     }
  30. }
  31.  
  32. ИЛИ:
  33.  
  34. import java.util.Scanner;
  35.  
  36. public class Main {
  37.     public static void main(String[] args) {
  38.         Scanner scanner = new Scanner(System.in);
  39.         int firstPlayer = Integer.parseInt(scanner.nextLine()),
  40.                 secondPlayer = Integer.parseInt(scanner.nextLine());
  41.         String command;
  42.         label:
  43.         while (true) {
  44.             command = scanner.nextLine();
  45.             switch (command) {
  46.                 case "End":
  47.                     System.out.println("Player one has " + firstPlayer + " eggs left.\n"
  48.                             + "Player two has " + secondPlayer + " eggs left.");
  49.                     break label;
  50.                 case "one":
  51.                     secondPlayer--;
  52.                     if (secondPlayer == 0) {
  53.                         System.out.println("Player two is out of eggs. Player one has " + firstPlayer + " eggs left.");
  54.                         break label;
  55.                     }
  56.                     break;
  57.                 case "two":
  58.                     firstPlayer--;
  59.                     if (firstPlayer == 0) {
  60.                         System.out.println("Player one is out of eggs. Player two has " + secondPlayer + " eggs left.");
  61.                         break label;
  62.                     }
  63.                     break;
  64.             }
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement