java 我的代码不是石头剪刀布[关闭]

hrysbysz  于 2023-05-21  发布在  Java
关注(0)|答案(1)|浏览(179)

**关闭。**这个问题是not reproducible or was caused by typos。目前不接受答复。

此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这一个是解决的方式不太可能帮助未来的读者。
昨天关门了。
Improve this question

import java.util.Random; // Importing the Random class from java.util package
import java.util.Scanner; // Importing the Scanner class from java.util package

 public class RockPaperScissors { // Defining a public class named Rock Paper Scissors
 
 public static void main(String[] args) { // Defining the main method

Scanner scanner = new Scanner(System.in); // Creating a new Scanner object for user input

while(true) { // Starting an infinite loop

String[] rps = {"r", "p", "s"}; // Creating an array with the possible moves: rock, paper, Scissors
String computerMove= rps[new Random().nextInt(rps.length)]; // Geneating a random move for the computer

String playerMove; // Declaring a variable to store the player's move

while(true) { // Starting another infinite loop for validating player input
System.out.println("Please enter your move (r, p, or s)"); // Prompting the player to  enter their nove
playerMove = scanner.nextLine(); // Reading the player's move from the console
if (playerMove.equals("r") || playerMove.equals("p") || playerMove.equals("s")) { // Checking if the player's move is valid
break; // Exiting the loop if the player's move is valid
}
System.out.println(playerMove + "is not a valid move." ); // Printing an error message for invalid moves
}

System.out.println("Computer played: " + computerMove); // Printing the computer's move

这就是错误:

ERROR!
javac /tmp/CckbE29PRk/RockPaperScissors.java
/tmp/CckbE29PRk/RockPaperScissors.java:27: error: reached end of file while parsing
}
^
1 error

请输入您的移动(r,p,或s)r计算机播放:你赢了!玩盖亚?(是/否)

8iwquhpp

8iwquhpp1#

您很可能忘记了while循环末尾的右括号(“}”)。这里是代码格式良好和固定.

import java.util.Random;
import java.util.Scanner;

public class RockPaperScissors {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            String[] rps = {"r", "p", "s"};
            String computerMove = rps[new Random().nextInt(rps.length)];

            String playerMove;

            while (true) {
                System.out.println("Please enter your move (r, p, or s)");
                playerMove = scanner.nextLine();
                if (playerMove.equals("r") || playerMove.equals("p") || playerMove.equals("s")) {
                    break;
                }
                System.out.println(playerMove + " is not a valid move.");
            }

            System.out.println("Computer played: " + computerMove);

            // Rest of your code 

        } // Closing brace for the while loop
    } // Closing brace for the main method
}

相关问题