|
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; |
6 | 3 |
|
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 | + } |
10 | 8 |
|
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]; |
13 | 14 | }
|
14 | 15 |
|
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; |
21 | 31 | }
|
22 | 32 |
|
23 |
| - return `You lose! ${computerSelection} beats ${playerSelection}`; |
24 |
| -} |
| 33 | + public static void playGame() { |
| 34 | + Scanner scanner = new Scanner(System.in); |
25 | 35 |
|
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(); |
32 | 45 | }
|
33 | 46 | }
|
34 |
| - |
35 |
| -game(); |
|
0 commit comments