使用 break in while 循环给我错误 "break cannot be used outside of a loop or a switch" 即使它已经在循环中

Using break in while loop is giving me error "break cannot be used outside of a loop or a switch" even though it is already inside a loop

public class rps {
    public static void main(String args[]) {
    int userCount = 0;
    int myCount = 0;
    int tieCount = 0; /* All 3 are counters that keep track of win scores */
    Scanner scan = new Scanner(System.in); /* Create a scanner */
    String play = ""; /* Scanner string local variable */   
do {    
    System.out.print("Please enter Play if you want to play the game or anything else to Stop");
    play = scan.nextLine(); // 
    }   
     while (play.equalsIgnoreCase("play")); {
        System.out.print("Choose your weapon [R]ock, [P]aper, or [S]cissors: ");
        String rps = scan.nextLine();
        while (rps.equals('R') || rps.equals('P') || rps.equals('S')) {
            System.out.println("You chose: " + rps);
        }
        int rand = (int)(Math.random() * 3);
        String myMove = "";
        if(rand == 0) {
            myMove = "Rock";
        }
        else if(rand == 1) {
            myMove = "Paper";
        }
        else {
            myMove = "Scissors";
        }
        System.out.println("I chose: " + myMove);
        
        
        if(rps.equals(myMove)) {
            System.out.println("Tie!");
            tieCount++;
        }
        else if(rps.equals('P') && myMove.equals('S')) {
            System.out.println("Scissors beats paper, a win for me!");
            myCount++;
        }
        else if(rps.equals('S') && myMove.equals('R')) {
            System.out.println("Rock beats scissors, a win for me!");
            myCount++;
        }
        else if(rps.equals('R') && myMove.equals('P')) {
            System.out.println("Paper beats rock, a win for me!");
            myCount++;
        }
        else if(rps.equals('S') && myMove.equals('P')) {
            System.out.println("Scissors beats paper, a win for you!");
            userCount++;
        }
        else if(rps.equals('R') && myMove.equals('S')) {
            System.out.println("Rock beats scissors, a win for you!");
            userCount++;
        }
        else if(rps.equals('S') && myMove.equals('P')) {
            System.out.println("Paper beats rock, a win for you!");
            userCount++;
        }
        
        System.out.println("Please enter Play if you want to play the game again or anything else to Stop.");
        if(!play.equalsIgnoreCase("play")) break;
    }
}

我怎样才能结束循环?它给我一个错误,它不能在循环外使用,但我很困惑,因为它仍在我的循环中?我认为这与它在 main 方法中的事实有关,但如果没有 main 方法,我的扫描仪代码会出现不同的错误。 (我应该制作一个剪刀石头布游戏,当用户选择停止时在循环结束时结束。我不知道如何解决这个错误。)

你的break实际上不在循环中。你应该看看 do-while 循环的语法。然后,你可以意识到循环在while().

后的分号后结束
do {
    // Do some stuff in a loop...
}
while(condition);    // <-- Loop ends here

while 循环和do-while 循环之间的相关区别在于,前者仅在给定条件提升为真时才进入,而后者总是至少进入一个。

while

  1. 检查条件是否应该执行循环中的语句
  2. 循环执行语句并转到开头

do-while

  1. 循环执行语句
  2. 检查可能重新开始的条件。

break确实在循环外。你的循环是这样的:

do {    
    System.out.print("Please enter Play if you want to play the game or anything else to Stop");
    play = scan.nextLine(); // 
} while (play.equalsIgnoreCase("play"));

你的意思可能是你的代码是这样的:

 // no do {
System.out.print("Please enter Play if you want to play the game or anything else to Stop");
play = scan.nextLine(); // 
 // no }

while (play.equalsIgnoreCase("play")) { // no semicolon!
    ...
}

更新

您似乎对 while 循环可以采用的两种形式感到困惑:

  1. do { <code> } while ( <condition> ) - 这里,代码出现在条件之前并且总是至少执行一次。

  2. while ( <condition>) { <code> } - 这里,条件在前,代码可能一次都不会执行。

您的示例应该看起来像这样才能工作:

Scanner scan = new Scanner(System.in); /* Create a scanner */

while (true) {    
    System.out.print("Please enter Play if you want to play the game or anything else to Stop");
    String play = scan.nextLine();

    if (!play.equalsIgnoreCase("play")) {
        break();
    }

    System.out.print("Choose your weapon [R]ock, [P]aper, or [S]cissors: ");
    ...
}      

你有一个 do-while 循环。

do {    
           System.out.print("Please enter Play if you want to play 
                    the game or anything else to Stop");
           play = scan.nextLine(); // 
} while (play.equalsIgnoreCase("play"))

而且你在这个循环中没有休息。

您的 break 语句超出循环。简而言之,您可能需要从一本好书中学习如何使用循环。

您似乎想在循环之前读取第一个值。所以这样做:

    //do { // no need do
    System.out.print("Please enter Play if you want to play the game or anything else to Stop");
    play = scan.nextLine(); // 
   // } no need do

    while (play.equalsIgnoreCase("play")) { // remove comma here
        System.out.print("Choose your weapon [R]ock, [P]aper, or [S]cissors: ");
        S

最终代码为:

    // these two lines allow to initialize "play" variable
    System.out.print("Please enter Play if you want to play the game or anything else to Stop");
    play = scan.nextLine();

    while (play.equalsIgnoreCase("play")) { // (**) test "play" var to keep in the loop
       // these 2 lines are good.
       System.out.print("Choose your weapon [R]ock, [P]aper, or [S]cissors: ");
       String rps = scan.nextLine();
        ...
        ...

        ...
        ...

        ...
        ...

        // at the end of the loop, read again "play" var to decides if you stay or stop loop
        System.out.println("Please enter Play if you want to play the game again or anything else to Stop.");
        play = scan.nextLine(); // IMPORTANT
        // but no need to break, because the play value will be tested by the enclosing while condition at (**)
        // if(!play.equalsIgnoreCase("play")) 
            // break;
    }