|
2 | 2 | * @author : Jonathan Birkey
|
3 | 3 |
|
4 | 4 | * @created : 28Feb2024
|
5 |
| - * <p>(Display leap years) Write a program that displays all the leap years, 10 per line, from |
6 |
| - * 101 to 2100, separated by exactly one space. Also display the number of leap years in this |
7 |
| - * period. |
| 5 | + * <p>(Game: scissor, rock, paper) Programming Exercise 3.17 gives a program that plays the |
| 6 | + * scissor–rock–paper game. Revise the program to let the user continuously play until either |
| 7 | + * the user or the computer wins more than two times than its opponent. |
8 | 8 | */
|
9 | 9 | package com.github.jonathanbirkey.chapter05;
|
10 | 10 |
|
| 11 | +import java.util.Scanner; |
| 12 | + |
11 | 13 | public class Exercise34 {
|
12 | 14 | public static void main(String[] args) {
|
13 |
| - // TODO: solve |
| 15 | + Scanner input = new Scanner(System.in); |
| 16 | + int computerWins = 0; |
| 17 | + int userWins = 0; |
| 18 | + |
| 19 | + while (computerWins < 2 && userWins < 2) { |
| 20 | + int computerPick = (int) (Math.random() * 3); |
| 21 | + System.out.print("scissor (0), rock (1), paper (2): "); |
| 22 | + int userPick = input.nextInt(); |
| 23 | + switch (computerPick) { |
| 24 | + case 0: |
| 25 | + System.out.print("The computer is scissor. "); |
| 26 | + break; |
| 27 | + case 1: |
| 28 | + System.out.print("The computer is rock. "); |
| 29 | + break; |
| 30 | + case 2: |
| 31 | + System.out.print("The computer is paper. "); |
| 32 | + break; |
| 33 | + default: |
| 34 | + System.out.print("Invalide"); |
| 35 | + } |
| 36 | + switch (userPick) { |
| 37 | + case 0: |
| 38 | + System.out.print("You are scissor"); |
| 39 | + break; |
| 40 | + case 1: |
| 41 | + System.out.print("You are rock"); |
| 42 | + break; |
| 43 | + case 2: |
| 44 | + System.out.print("You are paper"); |
| 45 | + break; |
| 46 | + default: |
| 47 | + System.out.print("Invalide"); |
| 48 | + } |
| 49 | + if (computerPick == userPick) { |
| 50 | + System.out.println(" too. It is a draw"); |
| 51 | + } else if (computerPick == 0 && userPick == 1) { |
| 52 | + System.out.println(". You won\n"); |
| 53 | + userWins++; |
| 54 | + } else if (computerPick == 1 && userPick == 2) { |
| 55 | + System.out.println(". You won\n"); |
| 56 | + userWins++; |
| 57 | + } else if (computerPick == 2 && userPick == 0) { |
| 58 | + System.out.println(". You won\n"); |
| 59 | + userWins++; |
| 60 | + } else if (computerPick == 0 && userPick == 2) { |
| 61 | + System.out.println(". You lost\n"); |
| 62 | + computerWins++; |
| 63 | + } else if (computerPick == 1 && userPick == 0) { |
| 64 | + System.out.println(". You lost\n"); |
| 65 | + computerWins++; |
| 66 | + } else if (computerPick == 2 && userPick == 1) { |
| 67 | + System.out.println(". You lost\n"); |
| 68 | + computerWins++; |
| 69 | + } |
| 70 | + } |
| 71 | + input.close(); |
| 72 | + System.out.printf("\nFinal score\nComputer: %d\tUser: %d", computerWins, userWins); |
14 | 73 | }
|
15 | 74 | }
|
0 commit comments