Skip to content

Commit e67ea10

Browse files
finised exercise 34
1 parent c3cd77d commit e67ea10

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

src/com/github/jonathanbirkey/chapter03/Exercise17.java

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static void main(String[] args) {
3131
break;
3232
case 2:
3333
System.out.print("The computer is paper. ");
34+
break;
3435
default:
3536
System.out.print("Invalide");
3637
}
@@ -43,6 +44,7 @@ public static void main(String[] args) {
4344
break;
4445
case 2:
4546
System.out.print("You are paper");
47+
break;
4648
default:
4749
System.out.print("Invalide");
4850
}

src/com/github/jonathanbirkey/chapter05/Exercise34.java

+63-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,73 @@
22
* @author : Jonathan Birkey
33
* @mailto : [email protected]
44
* @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.
88
*/
99
package com.github.jonathanbirkey.chapter05;
1010

11+
import java.util.Scanner;
12+
1113
public class Exercise34 {
1214
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);
1473
}
1574
}

0 commit comments

Comments
 (0)