System.out.println - 语法错误

时间:2014-06-08 15:31:01

标签: java

Java中的初学者。

试图弄清楚为什么我在最后两个System.out.println上出现语法错误。

我尝试移动括号,但我最终得到一个永无止境的循环。

我提前感谢您的所有帮助!


import java.util.Scanner;

public class GuessingGame 
{

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

        int random = (int) (Math.random()*20 +1);
        int numberOfAttempts = 0;
        boolean win = false;

        while (win == false)

            System.out.println("Guess the number (1 - 20)");
            int guess = scan.nextInt();
            numberOfAttempts++;

            if (guess == random)
                win = true;
            else if (guess < random)
                System.out.println("Your guess was low. Guess again.");
            else
                System.out.println("Your guess was high. Guess again.");
    }

            System.out.println("You guessed right! It took you guesses.");  
            System.out.println("Do you want to play again? (yes or no)");

}

1 个答案:

答案 0 :(得分:1)

你没有打开while循环。在while之后你需要括号

while (win == false){
//need one at the beginning 

        System.out.println("Guess the number (1 - 20)");
        int guess = scan.nextInt();
        numberOfAttempts++;

        if (guess == random)
            win = true;
        else if (guess < random)
            System.out.println("Your guess was low. Guess again.");
        else
            System.out.println("Your guess was high. Guess again.");
} //and at the end