-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRockpaperscissor.java
More file actions
97 lines (87 loc) · 3.7 KB
/
Copy pathRockpaperscissor.java
File metadata and controls
97 lines (87 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.Scanner;
public class Rockpaperscissor {
Scanner in = new Scanner(System.in);
String username = "";
int userScore = 0;
int computerScore = 0;
Rockpaperscissor() {
System.out.println("Enter your name:");
String user = in.nextLine();
System.out.println("Welcome " + user);
username = user;
}
void play() {
int userChoice;
do {
System.out.println("Choose an option ..");
System.out.println("1 : Rock , 2 : Paper , 3 : Scissors , 4 : Quit");
userChoice = in.nextInt();
if (userChoice == 4) {
System.out.println("Goodbye, " + username + "!");
break;
}
int computerChoice = (int) (Math.random() * 3) + 1;
switch (userChoice) {
case 1:
switch (computerChoice) {
case 1:
System.out.println(username + " Rock - Computer Rock (TIE)");
break;
case 2:
System.out.println(username + " Rock - Computer Paper (Computer wins)");
computerScore++;
break;
case 3:
System.out.println(username + " Rock - Computer Scissors (" + username + " wins)");
userScore++;
break;
default:
System.out.println("Enter a valid input!");
}
break;
case 2:
switch (computerChoice) {
case 1:
System.out.println(username + " Paper - Computer Rock (" + username + " wins)");
userScore++;
break;
case 2:
System.out.println(username + " Paper - Computer Paper (TIE)");
break;
case 3:
System.out.println(username + " Paper - Computer Scissors (Computer wins)");
computerScore++;
break;
default:
System.out.println("Enter a valid input!");
}
break;
case 3:
switch (computerChoice) {
case 1:
System.out.println(username + " Scissors - Computer Rock (Computer wins)");
computerScore++;
break;
case 2:
System.out.println(username + " Scissors - Computer Paper (" + username + " wins)");
userScore++;
break;
case 3:
System.out.println(username + " Scissors - Computer Scissors (TIE)");
break;
default:
System.out.println("Enter a valid input!");
}
break;
default:
System.out.println("Please choose a valid option.");
}
System.out.println("Score: " + username + " " + userScore + " - Computer " + computerScore);
} while (userChoice != 4);
System.out.println("Final Score: " + username + " " + userScore + " - Computer " + computerScore);
}
public static void main(String[] args) {
Rockpaperscissor game = new Rockpaperscissor();
game.play();
}
}