-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathRockPaperScissors.java
More file actions
56 lines (45 loc) · 2.06 KB
/
RockPaperScissors.java
File metadata and controls
56 lines (45 loc) · 2.06 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
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();
String[] choices = {"Rock", "Paper", "Scissors"};
int playerWins = 0, computerWins = 0;
System.out.println("Welcome to Rock, Paper, Scissors!");
System.out.println("Enter 'Rock', 'Paper', or 'Scissors'. Type 'exit' to quit the game.");
while (true) {
System.out.print("Your move: ");
String playerMove = scanner.nextLine().trim().toLowerCase();
if (playerMove.equals("exit")) {
break;
}
if (!playerMove.equals("rock") && !playerMove.equals("paper") && !playerMove.equals("scissors")) {
System.out.println("Invalid input. Please try again.");
continue;
}
// Computer randomly selects Rock, Paper, or Scissors
String computerMove = choices[random.nextInt(3)];
System.out.println("Computer chose: " + computerMove);
// Determine the winner
if (playerMove.equals(computerMove.toLowerCase())) {
System.out.println("It's a tie!");
} else if (
(playerMove.equals("rock") && computerMove.equals("Scissors")) ||
(playerMove.equals("paper") && computerMove.equals("Rock")) ||
(playerMove.equals("scissors") && computerMove.equals("Paper"))
) {
System.out.println("You win this round!");
playerWins++;
} else {
System.out.println("Computer wins this round!");
computerWins++;
}
// Display the score
System.out.println("Score -> You: " + playerWins + " | Computer: " + computerWins);
}
System.out.println("Final Score -> You: " + playerWins + " | Computer: " + computerWins);
System.out.println("Thanks for playing!");
scanner.close();
}
}