Repository navigation
Expand file tree
/
Copy pathrockPaperScissors.java
More file actions
47 lines (43 loc) · 1.7 KB
/
Copy pathrockPaperScissors.java
File metadata and controls
47 lines (43 loc) · 1.7 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.Random;
import java.util.Scanner;
public class rockPaperScissors {
public static void main(String[] args){
//Rock paper scissors game
// declare variables
//get choice from the user
//get random choice for the computer
//ask to play again
//goodbye
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("scissor"))){
System.out.println("Invalid Choice!");
continue;
}
computerChoice = choices[random.nextInt(3)];
System.out.println("Computer coice : "+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("You want to play again(yes/no):");
playAgain = scanner.nextLine().toLowerCase();
} while (playAgain.equals("yes"));
System.out.println("Thanks for playing.");
scanner.close();
}
}