Skip to content

Commit ce82380

Browse files
authored
Update RockPaperScissor.java
JavaScript to java code
1 parent ec88c8a commit ce82380

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

RockPaperScissor.js

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,46 @@
1-
function computerPlay() {
2-
const choices = ["Rock", "Paper", "Scissors"];
3-
const randomIndex = Math.floor(Math.random() * 3);
4-
return choices[randomIndex];
5-
}
1+
import java.util.Scanner;
2+
import java.util.Random;
63

7-
function playRound(playerSelection, computerSelection) {
8-
playerSelection = playerSelection.toLowerCase();
9-
computerSelection = computerSelection.toLowerCase();
4+
public class RockPaperScissors {
5+
public static void main(String[] args) {
6+
playGame();
7+
}
108

11-
if (playerSelection === computerSelection) {
12-
return "It's a tie!";
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];
1314
}
1415

15-
if (
16-
(playerSelection === "rock" && computerSelection === "scissors") ||
17-
(playerSelection === "scissors" && computerSelection === "paper") ||
18-
(playerSelection === "paper" && computerSelection === "rock")
19-
) {
20-
return `You win! ${playerSelection} beats ${computerSelection}`;
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;
2131
}
2232

23-
return `You lose! ${computerSelection} beats ${playerSelection}`;
24-
}
33+
public static void playGame() {
34+
Scanner scanner = new Scanner(System.in);
2535

26-
function game() {
27-
for (let i = 0; i < 5; i++) {
28-
const playerSelection = prompt("Rock, Paper, or Scissors?").trim();
29-
const computerSelection = computerPlay();
30-
const roundResult = playRound(playerSelection, computerSelection);
31-
console.log(roundResult);
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();
3245
}
3346
}
34-
35-
game();

0 commit comments

Comments
 (0)