-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminiproject.java
More file actions
60 lines (50 loc) · 2.61 KB
/
Copy pathminiproject.java
File metadata and controls
60 lines (50 loc) · 2.61 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
57
58
59
60
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[] options = {"Rock", "Paper", "Scissors"};
int userScore = 0;
int computerScore = 0;
System.out.println("Welcome to Rock, Paper, Scissors!");
System.out.println("Enter your choice (Rock, Paper, or Scissors). Type 'exit' to quit.");
while (true) {
System.out.print("Your turn: ");
String userChoice = scanner.nextLine();
if (userChoice.equalsIgnoreCase("exit")) {
System.out.println("\nGame Over!");
System.out.println("Final Score:");
System.out.println("User: " + userScore + " | Computer: " + computerScore);
if (userScore > computerScore) {
System.out.println("Congratulations! You won the game!");
} else if (computerScore > userScore) {
System.out.println("Computer wins! Better luck next time.");
} else {
System.out.println("It's a tie overall!");
}
break;
}
int computerChoiceIndex = random.nextInt(3);
String computerChoice = options[computerChoiceIndex];
System.out.println("Computer chose: " + 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 this round!");
userScore++;
} else if ((userChoice.equalsIgnoreCase("Rock") && computerChoice.equals("Paper")) ||
(userChoice.equalsIgnoreCase("Paper") && computerChoice.equals("Scissors")) ||
(userChoice.equalsIgnoreCase("Scissors") && computerChoice.equals("Rock"))) {
System.out.println("Computer wins this round!");
computerScore++;
} else {
System.out.println("Invalid input, please enter Rock, Paper, or Scissors.");
}
System.out.println(); // Blank line for readability
}
scanner.close();
}
}