无法从代码中删除一行?

时间:2013-02-20 03:16:14

标签: java

该应用程序是一个数字猜测游戏我的问题是当用户获得正确的随机数时,它询问用户是否要继续如果他输入“y”它会创建一个新的随机数并要求用户猜测它。然后它应该询问用户“输入一个数字”。相反,我得到

“太高”(或“太低”)
“输入数字”

ex输出:
输入数字:
2
纠正你已经得到它!数字是2 你有两次尝试 你想再玩一次(是/否):
ÿ
太低!再试一次。
输入数字:

如何在没有打印“太高”或“太低”的情况下得到它,这可能是在他输入数字后确定的?

PS。我尝试了很多方法,但卡住了:(

public static void main(String[] args) {
    System.out
            .println("Welcome to the gussing game, Try to guess the number am thinking of to win!");
    System.out
            .println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    System.out.println();

    System.out
            .println("Am thinking of a number between 0 and 10. Try to guess it.");
    System.out.println();

    Scanner sc = new Scanner(System.in);
    String choice = "y";

    double rightNum = Math.random() * 10;
    int randomNum = (int) rightNum; // convert the random number to int
    int tries = 0;

    while (!choice.equalsIgnoreCase("n")) {

        System.out.println("Enter the Number:");
        int guess = sc.nextInt();
        tries++;

        if (guess == randomNum) {
            System.out.println("Correct you've got it! The Number Was "
                    + randomNum);
            System.out.println("You got it in " + tries + " tries.");
            System.out.println("Would you like to play again (y/n):");
            choice = sc.next();

            if (choice.equalsIgnoreCase("y"))
            // reset the random number

            {
                rightNum = Math.random() * 10;
                randomNum = (int) rightNum;
                tries = 0;

            }
        }

        if (guess > randomNum + 10) {
            System.out.println("Way to high! Try again.");
        } else if (guess < randomNum) {
            System.out.println("Too low! Try again.");
        } else if (guess > randomNum && guess <= randomNum + 10) {
            System.out.println("Too high! Try again.");
        }

    }

}

}

2 个答案:

答案 0 :(得分:9)

else if (guess > randomNum + 10)
{
    System.out.println("Way to high! Try again.");
} 

你错过了其他人。

答案 1 :(得分:0)

更改此

{
    rightNum = Math.random() * 10;
    randomNum = (int) rightNum;
    tries = 0;
}

{
    rightNum = Math.random() * 10;
    randomNum = (int) rightNum;
    tries = 0;
    continue; /* this goes back to while loop */
}