尽管在循环内,但在循环外继续错误

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


我在网上查了一下,发现人们因为多了一个分号而遇到了问题。我没有那个问题,但我仍然收到此错误。


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: 在循环外继续


月关宝盒
浏览 136回答 4
4回答

狐的传说

从语言规范:语句&nbsp;continue&nbsp;只能出现在&nbsp;while,&nbsp;&nbsp;do, or&nbsp;for&nbsp;语句中;这三种语句称为&nbsp;迭代语句。你没有使用这些。if语句不是循环。编译器没有骗你!如果您想使用 a&nbsp;continue,请围绕您要重复的代码放置一个循环。

qq_遁去的一_1

希望这是你期待的逻辑......public class GuessingGame {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int random;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; random = (int) (Math.random() * 10) + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter you guess between 1 to 10");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int guess = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (guess != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (guess < random) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You guessed too low!"+random);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter you guess between 1 to 10");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guess = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (guess > random) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You guessed too High"+random);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter you guess between 1 to 10");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guess = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (guess == random) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You guessed it right, the number is " + random);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }}

慕哥6287543

像这样尝试import java.util.Scanner;public class GuessingGame {&nbsp; &nbsp; int random;&nbsp; &nbsp; public GuessingGame() {&nbsp; &nbsp; &nbsp; &nbsp; generateNumber();&nbsp; &nbsp; &nbsp; &nbsp; guess();&nbsp; &nbsp; }&nbsp; &nbsp; public void generateNumber() {&nbsp; &nbsp; &nbsp; &nbsp; // The following lines generate and output a random number between 1 and&nbsp; &nbsp; &nbsp; &nbsp; // 10&nbsp; &nbsp; &nbsp; &nbsp; random = (int) (Math.random() * 10) + 1;&nbsp; &nbsp; }&nbsp; &nbsp; // Write the guess() method below&nbsp; &nbsp; public void guess() {&nbsp; &nbsp; &nbsp; &nbsp; // Use scanner to accept a user input&nbsp; &nbsp; &nbsp; &nbsp; // Create a new scanner object to receive user input&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter you guess between 1 to 10");&nbsp; &nbsp; &nbsp; &nbsp; int guess = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; while (guess != random) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // write your code below&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (guess < random) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You guessed too low!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You guessed too high");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guess = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You guessed it right, the number is " + random);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; new GuessingGame();&nbsp; &nbsp; }}

莫回无

在 java 中,continue 一词仅在循环内使用,并在不考虑 continue 语句下面的代码的情况下开始循环中的下一次迭代。你这里的错误是你根本没有使用循环,所以 java 编译器不知道如何处理 continue 语句。我假设您在完成后继续退出 if 语句。如果是这样,那么您可以完全删除 continue 语句,它应该会运行。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java