-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrockpaper.java
More file actions
48 lines (38 loc) · 1.66 KB
/
rockpaper.java
File metadata and controls
48 lines (38 loc) · 1.66 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
import java.util.Random;
import java.util.Scanner;
public class rockpaper {
public static void main(String[] args) {
// Rock Paper Scissors Game
Scanner scanner = new Scanner(System.in);
Random random = new Random();
String[] choices = {"rock", "paper", "scissors"};
String playerChoice;
String computerChoice;
String playAgain = "yes";
do {
System.out.print("Enter your move (rock, paper, scissors): ");
playerChoice = scanner.nextLine().toLowerCase();
if(!playerChoice.equals("rock") &&
!playerChoice.equals("paper") &&
!playerChoice.equals("scissors")) {
System.out.println("Please enter a valid choice!");
continue;
}
computerChoice = choices[random.nextInt(0, 3)];
System.out.println("Computer choice: " + 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("You lose!");
}
System.out.print("Play Again? (yes/no): ");
playAgain = scanner.nextLine().toLowerCase();
} while (playAgain.equals("yes"));
System.out.println("Goodbye!");
scanner.close();
}
}