Skip to content

Commit 85b133b

Browse files
Merge pull request #387 from SoulNikhar/patch-1
Create RockPaperScissor.js
2 parents f554479 + ce82380 commit 85b133b

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

RockPaperScissor.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.Scanner;
2+
import java.util.Random;
3+
4+
public class RockPaperScissors {
5+
public static void main(String[] args) {
6+
playGame();
7+
}
8+
9+
public static String computerPlay() {
10+
String[] choices = {"Rock", "Paper", "Scissors"};
11+
Random random = new Random();
12+
int randomIndex = random.nextInt(3);
13+
return choices[randomIndex];
14+
}
15+
16+
public static String playRound(String playerSelection, String computerSelection) {
17+
playerSelection = playerSelection.toLowerCase();
18+
computerSelection = computerSelection.toLowerCase();
19+
20+
if (playerSelection.equals(computerSelection)) {
21+
return "It's a tie!";
22+
}
23+
24+
if ((playerSelection.equals("rock") && computerSelection.equals("scissors")) ||
25+
(playerSelection.equals("scissors") && computerSelection.equals("paper")) ||
26+
(playerSelection.equals("paper") && computerSelection.equals("rock"))) {
27+
return "You win! " + playerSelection + " beats " + computerSelection;
28+
}
29+
30+
return "You lose! " + computerSelection + " beats " + playerSelection;
31+
}
32+
33+
public static void playGame() {
34+
Scanner scanner = new Scanner(System.in);
35+
36+
for (int i = 0; i < 5; i++) {
37+
System.out.print("Rock, Paper, or Scissors? ");
38+
String playerSelection = scanner.nextLine().trim();
39+
String computerSelection = computerPlay();
40+
String roundResult = playRound(playerSelection, computerSelection);
41+
System.out.println(roundResult);
42+
}
43+
44+
scanner.close();
45+
}
46+
}

0 commit comments

Comments
 (0)