-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathJavaGame.java
More file actions
47 lines (40 loc) · 1.87 KB
/
Copy pathJavaGame.java
File metadata and controls
47 lines (40 loc) · 1.87 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.Scanner;
import java.util.Random;
public class JavaGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
String[] choices = {"Stone", "Paper", "Scissors"};
System.out.println("Welcome to Stone, Paper, Scissors Game!");
System.out.println("Enter your choice (Stone, Paper, Scissors):");
while (true) {
System.out.print("Your choice: ");
String userChoice = scanner.nextLine().trim();
if (!userChoice.equalsIgnoreCase("Stone") &&
!userChoice.equalsIgnoreCase("Paper") &&
!userChoice.equalsIgnoreCase("Scissors")) {
System.out.println("Invalid choice. Please choose Stone, Paper, or Scissors.");
continue;
}
int computerIndex = random.nextInt(3);
String computerChoice = choices[computerIndex];
System.out.println("Computer chose: " + computerChoice);
if (userChoice.equalsIgnoreCase(computerChoice)) {
System.out.println("It's a tie!");
} else if ((userChoice.equalsIgnoreCase("Stone") && computerChoice.equals("Scissors")) ||
(userChoice.equalsIgnoreCase("Paper") && computerChoice.equals("Stone")) ||
(userChoice.equalsIgnoreCase("Scissors") && computerChoice.equals("Paper"))) {
System.out.println("You win!");
} else {
System.out.println("You lose!");
}
System.out.print("Do you want to play again? (yes/no): ");
String playAgain = scanner.nextLine().trim();
if (!playAgain.equalsIgnoreCase("yes")) {
System.out.println("Thanks for playing!");
break;
}
}
scanner.close();
}
}