尽管在循环错误中,但仍在循环错误之外继续

时间:2019-07-02 19:05:26

标签: java loops continue

由于外部循环继续错误,我无法运行此代码。

我在网上看过,发现人们由于使用多余的分号而遇到了问题。我没有这个问题,但是仍然出现此错误。

public class GuessingGame {

     int random;

    public void generateNumber() 
    {
       //The following lines generate and output a random number between 1 and 10
        random = (int)(Math.random()*10)+1;

    }

   //Write the guess() method below 
   public void guess()
   {
       //Use scanner to accept a user input 
       //Create a new scanner object to receive user input
          Scanner sc=new Scanner(System.in);
          System.out.println("Enter you guess between 1 to 10");
          int guess = sc.nextInt();

          //write your code below
       if (guess == random){
           System.out.println("You guessed it right, the number is " + random);

       } else if (guess<random) {
           System.out.println("You guessed too low!");
           continue;

       } else {
           System.out.println("You guessed too low");
           continue;

       }
   }

我每次运行都会收到此错误。错误:(35、12)Java:在循环外继续

4 个答案:

答案 0 :(得分:1)

来自language spec

  

A continue声明只能在whiledofor声明中出现;这三种类型的语句称为 iteration语句

您没有使用任何这些。 if语句不是循环。编译器没有骗你!

如果要使用continue,请在要重复的代码周围加一个循环。

答案 1 :(得分:0)

尝试这样

import java.util.Scanner;

public class GuessingGame {

    int random;

    public GuessingGame() {

        generateNumber();

        guess();
    }

    public void generateNumber() {
        // The following lines generate and output a random number between 1 and
        // 10
        random = (int) (Math.random() * 10) + 1;

    }

    // Write the guess() method below
    public void guess() {
        // Use scanner to accept a user input
        // Create a new scanner object to receive user input
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter you guess between 1 to 10");
        int guess = sc.nextInt();

        while (guess != random) {
            // write your code below
            if (guess < random) {
                System.out.println("You guessed too low!");

            } else {
                System.out.println("You guessed too high");

            }
            guess = sc.nextInt();
        }
        System.out.println("You guessed it right, the number is " + random);
    }

    public static void main(String[] args) {
        new GuessingGame();
    }
}

答案 2 :(得分:0)

在Java中,“继续”一词仅在循环内使用,并在循环中开始下一次迭代,而无需考虑continue语句下面的代码。 这里的错误是您根本不使用循环,因此Java编译器不知道如何使用continue语句。

我假设您在完成后放入了if语句的继续。如果是这样,那么您可以完全删除继续语句,它应该运行。

答案 3 :(得分:0)

希望这是您期望的逻辑。...

public class GuessingGame {



    public static void main(String[] args) {

            int random;

            random = (int) (Math.random() * 10) + 1;

            Scanner sc = new Scanner(System.in);
            System.out.println("Enter you guess between 1 to 10");
            int guess = sc.nextInt();

            while (guess != 0) {
                if (guess < random) {
                    System.out.println("You guessed too low!"+random);
                    System.out.println("Enter you guess between 1 to 10");
                    guess = sc.nextInt();
                    continue;
                } else if (guess > random) {
                    System.out.println("You guessed too High"+random);
                    System.out.println("Enter you guess between 1 to 10");
                    guess = sc.nextInt();
                    continue;
                }else{
                    if (guess == random) {
                        System.out.println("You guessed it right, the number is " + random);
                    }
                    break;
                }

            }

        }

}