SHARE
TWEET

Untitled

a guest Oct 21st, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class E06_CardsGame {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         List<Integer> firstPlayerCards = Arrays.stream(scanner.nextLine().split("\\s+"))
  10.                 .map(Integer::parseInt).collect(Collectors.toList());
  11.         List<Integer> secondPlayerCards = Arrays.stream(scanner.nextLine().split("\\s+"))
  12.                 .map(Integer::parseInt).collect(Collectors.toList());
  13.  
  14.  
  15.         while (firstPlayerCards.size() != 0 && secondPlayerCards.size() != 0) {
  16.             int firstPlayerCard = firstPlayerCards.get(0);
  17.             int secondPlayerCard = secondPlayerCards.get(0);
  18.  
  19.             if (firstPlayerCard > secondPlayerCard) {
  20.                 firstPlayerCards.add(firstPlayerCard);
  21.                 firstPlayerCards.add(secondPlayerCard);
  22.                 secondPlayerCards.remove(secondPlayerCards.get(0));
  23.                 firstPlayerCards.remove(0);
  24.  
  25.             } else if (secondPlayerCard > firstPlayerCard) {
  26.                 secondPlayerCards.add(secondPlayerCard);
  27.                 secondPlayerCards.add(firstPlayerCard);
  28.                 firstPlayerCards.remove(firstPlayerCards.get(0));
  29.                 secondPlayerCards.remove(0);
  30.  
  31.             } else {
  32.                 firstPlayerCards.remove(0);
  33.                 secondPlayerCards.remove(0);
  34.             }
  35.         }
  36.  
  37.         int firstPLayerSum = 0;
  38.         int secondPLayerSum = 0;
  39.         if (firstPlayerCards.size() != 0) {
  40.             for (Integer firstPlayerCardSum : firstPlayerCards) {
  41.                 firstPLayerSum += firstPlayerCardSum;
  42.             }
  43.             System.out.printf("First player wins! Sum: %d", firstPLayerSum);
  44.         } else {
  45.             for (Integer secondPlayerCardSum : secondPlayerCards) {
  46.                 secondPLayerSum += secondPlayerCardSum;
  47.             }
  48.             System.out.printf("Second player wins! Sum: %d", secondPLayerSum);
  49.         }
  50.  
  51.     }
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top