-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRockPaperScissors.java
More file actions
45 lines (36 loc) · 1.67 KB
/
RockPaperScissors.java
File metadata and controls
45 lines (36 loc) · 1.67 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
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
String[] choices = {"Rock", "Paper", "Scissors"};
System.out.println("Welcome to Rock, Paper, Scissors!");
System.out.println("Enter your choice (Rock, Paper, Scissors). Type 'exit' to quit.");
while (true) {
System.out.print("Your choice: ");
String userChoice = scanner.nextLine().trim();
if (userChoice.equalsIgnoreCase("exit")) {
System.out.println("Thanks for playing! Goodbye!");
break;
}
int computerIndex = random.nextInt(3);
String computerChoice = choices[computerIndex];
System.out.println("Computer's choice: " + computerChoice);
if (userChoice.equalsIgnoreCase(computerChoice)) {
System.out.println("It's a tie!");
} else if (
(userChoice.equalsIgnoreCase("Rock") && computerChoice.equals("Scissors")) ||
(userChoice.equalsIgnoreCase("Paper") && computerChoice.equals("Rock")) ||
(userChoice.equalsIgnoreCase("Scissors") && computerChoice.equals("Paper"))
) {
System.out.println("You win!");
} else {
System.out.println("You lose!");
}
#HLO
System.out.println("-----------");
}
scanner.close();
}
}