-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRockPaperScissors.java
More file actions
47 lines (37 loc) · 1.7 KB
/
RockPaperScissors.java
File metadata and controls
47 lines (37 loc) · 1.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
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.println("Let's play Rock, Paper, Scissors!");
while (true) {
System.out.print("Enter your choice (rock, paper, or scissors): ");
String playerChoice = scanner.nextLine().toLowerCase();
if (!playerChoice.equals("rock") && !playerChoice.equals("paper") && !playerChoice.equals("scissors")) {
System.out.println("Invalid choice. Please try again.");
continue;
}
String[] choices = {"rock", "paper", "scissors"};
String computerChoice = choices[random.nextInt(choices.length)];
System.out.println("You chose " + playerChoice + ". The computer chose " + computerChoice + ".");
if (playerChoice.equals(computerChoice)) {
System.out.println("It's a tie!");
} else if (
(playerChoice.equals("rock") && computerChoice.equals("scissors")) ||
(playerChoice.equals("paper") && computerChoice.equals("rock")) ||
(playerChoice.equals("scissors") && computerChoice.equals("paper"))
) {
System.out.println("You win!");
} else {
System.out.println("The computer wins!");
}
System.out.print("Do you want to play again? (y/n): ");
String playAgain = scanner.nextLine().toLowerCase();
if (!playAgain.equals("y")) {
break;
}
}
scanner.close();
}
}