-
Notifications
You must be signed in to change notification settings - Fork 113
/
Stone, Paper, Scissors.java
54 lines (42 loc) · 1.91 KB
/
Stone, Paper, Scissors.java
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
import java.util.Random;
import java.util.Scanner;
public class StonePaperScissors {
public static String determineWinner(String userChoice, String computerChoice) {
if (userChoice.equals(computerChoice)) {
return "It's a tie!";
}
switch (userChoice) {
case "stone":
return (computerChoice.equals("scissors")) ? "You win!" : "Computer wins!";
case "paper":
return (computerChoice.equals("stone")) ? "You win!" : "Computer wins!";
case "scissors":
return (computerChoice.equals("paper")) ? "You win!" : "Computer wins!";
default:
return "Invalid input.";
}
}
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!");
while (true) {
System.out.print("Enter your choice (stone, paper, scissors) or 'exit' to quit: ");
String userChoice = scanner.next().toLowerCase();
if (userChoice.equals("exit")) {
System.out.println("Thanks for playing! Goodbye.");
break;
}
if (!userChoice.equals("stone") && !userChoice.equals("paper") && !userChoice.equals("scissors")) {
System.out.println("Invalid choice. Please choose stone, paper, or scissors.");
continue;
}
String computerChoice = choices[random.nextInt(3)];
System.out.println("Computer's choice: " + computerChoice);
String result = determineWinner(userChoice, computerChoice);
System.out.println(result);
}
scanner.close();
}
}