-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStonePaperScissor.java
More file actions
53 lines (43 loc) · 1.94 KB
/
StonePaperScissor.java
File metadata and controls
53 lines (43 loc) · 1.94 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
import java.util.Scanner;
import java.util.Random;
public class StonePaperScissor {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random ra = new Random();
String[] choices = {"Stone", "Paper", "Scissor"};
while(true) {
System.out.println("Enter 1 for Stone, 2 for Paper and 3 for Scissor : ");
int userChoice = sc.nextInt();
if (userChoice < 1 || userChoice > 3) {
System.out.println("Oops! That\’s not a valid choice. Try again with 1, 2, or 3.");
continue;
}
userChoice -= 1;
int computerChoice = ra.nextInt(3);
System.out.println("You chose " + choices[userChoice] + ".");
System.out.println("Computer chose " + choices[computerChoice] + ".");
if (userChoice == computerChoice) {
System.out.println("We both chose the same! It's a tie!");
}
else if (userChoice == 0 && computerChoice == 2) {
System.out.println("Well done! You beat the computer!");
}
else if (userChoice == 1 && computerChoice == 0) {
System.out.println("Well done! You beat the computer!");
}
else if (userChoice == 2 && computerChoice == 1) {
System.out.println("Well done! You beat the computer!");
}
else {
System.out.println("Computer wins! Better luck next time!");
}
System.out.println("Do you want to play again? (yes/no)");
String playAgain = sc.next();
if (playAgain.equalsIgnoreCase("no")) {
System.out.println("Thanks for playing! See you next time!");
break;
}
}
sc.close();
}
}