Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package _16_ListsExercises;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class _06_CardsGame {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Integer> first = Arrays.stream(scanner.nextLine().split("\\s+"))
- .map(Integer::parseInt).collect(Collectors.toList());
- List<Integer> second = Arrays.stream(scanner.nextLine().split("\\s+"))
- .map(Integer::parseInt).collect(Collectors.toList());
- int count = first.size();
- while ((first.size() > 0 && second.size() > 0) || count > 0) {
- if (first.get(0) > second.get(0)) {
- first.add(first.get(0));
- first.add(second.get(0));
- first.remove(0);
- second.remove(0);
- } else if (second.get(0) > first.get(0)) {
- second.add(second.get(0));
- second.add(first.get(0));
- first.remove(0);
- second.remove(0);
- } else {
- first.remove(0);
- second.remove(0);
- }
- count--;
- }
- String player = "";
- int sum = 0;
- if (first.size() > 0) {
- for (Integer card : first) {
- sum += card;
- }
- player = "First player wins! Sum: ";
- } else {
- for (Integer card : second) {
- sum += card;
- }
- player = "Second player wins! Sum: ";
- }
- System.out.println(player + sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement