正确猜测的打印语句不打印

问题描述 投票:0回答:1

我正在创建一个数字猜谜游戏,但在使用 (do, while) 循环时遇到了问题。用户将输入他们的猜测,程序会告诉他们他们的猜测是太高还是太低。如果猜测不在 1 到 100 之间,程序将提示一条消息,说明只有 1 到 100 之间的数字有效,并且不计入他们有限的尝试次数。

我还没有开始工作的部分是当用户猜测正确的数字并且程序打印他们的分数或者他们是否已经用完或尝试。

static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        boolean keepPlaying = true;
        System.out.println("Play Guessing Game");
        while (keepPlaying) {
            boolean validInput;
            int number, playerGuess;
            String answer;
            int tryCount = (0) + 1;
            int counter = (10) - 1;

            number = (int) (Math.random() * 100) + 1;

            System.out.print("\nim thinking of a number between 1 and 100");

            do {
                playerGuess = sc.nextInt();
                validInput = true;
                if ((playerGuess < 1) || (playerGuess > 100)) {
                    System.out.println("Only numbers between 1 and 100");
                } else if (number > playerGuess && tryCount < (9)) {
                    System.out.println(" Guess higher " + " you have " + (counter--) + " attempts");
                    tryCount++;
                } else if (number < playerGuess && tryCount < (9)) {
                    System.out.println(" Guess lower " + " you have " + (counter--) + " attempts");
                    tryCount++;
                } else if (number < playerGuess && tryCount == (9)) {
                    System.out.println("Guess lower" + " you have " + (counter--) + " attempt ");
                    tryCount++;
                } else if (number > playerGuess && tryCount == (9)) {
                    System.out.println("Guess higher" + " you have " + (counter--) + " attempt ");
                    tryCount++;
                }
                validInput = false;

            } while (!validInput);

            if (playerGuess == number) {
                if (playerGuess == number && tryCount < (2)) {
                    System.out.println("Very Nice!! You're one lucky person! " + " It took you " + tryCount + " attempts");
                } else if (playerGuess == number && tryCount < (4)) {
                    System.out.println("Great score!!! " + " It took you " + tryCount + " attempts");
                } else if (playerGuess == number && tryCount < (6)) {
                    System.out.println("Good score " + " It took you " + tryCount + " attempts");
                } else if (playerGuess == number && tryCount < (11)) {
                    System.out.println(" correct " + " It took you " + tryCount + " attempts ");
                } else if (number > playerGuess && tryCount < (9)) {
                    System.out.println(" Guess higher " + " you have " + (counter--) + " attempts");
                } else if (number < playerGuess && tryCount < (9)) {
                    System.out.println(" Guess lower " + " you have " + (counter--) + " attempts");
                } else if (number < playerGuess && tryCount == (9)) {
                    System.out.println("Guess lower" + " you have " + (counter--) + " attempt ");
                } else if (number > playerGuess && tryCount == (9)) {
                    System.out.println("Guess higher" + " you have " + (counter--) + " attempt ");
                } else if (number > playerGuess && tryCount < (9)) {
                    System.out.println(" Guess higher " + " you have " + (counter--) + " attempts");
                    tryCount++;
                } else if (number < playerGuess && tryCount < (9)) {
                    System.out.println(" Guess lower " + " you have " + (counter--) + " attempts");
                    tryCount++;
                } else if (number < playerGuess && tryCount == (9)) {
                    System.out.println("Guess lower" + " you have " + (counter--) + " attempt ");
                    tryCount++;
                } else if (number > playerGuess && tryCount == (9)) {
                    System.out.println("Guess higher" + " you have " + (counter--) + " attempt ");
                    tryCount++;
                }
            }
        }

    }

}

java loops printf do-while println
1个回答
0
投票

这段代码中有很多错误。让我们一一过一遍。

int tryCount = (0) + 1;

上面这行代码有效地将

1
分配给了
tryCount
,甚至在用户还没有猜到任何东西之前。你可能想把它改成

int triesCount = 0;

int counter = (10) - 1;

我不确定这个变量代表什么意思,因为名字没有表达意思。它是允许倒计时的最大尝试次数计数器吗?

在那种情况下,您可能想将其更改为

int triesLeft = 10;

do {
  validInput = true;
  ...
  validInput = false;
} while (!validInput)

此块将无限运行,因为您在每次循环迭代中将值设置为

false
true
的第一个赋值是多余的。

我不确定你到底想在这里做什么,所以我不能建议任何更正,抱歉。是判断用户输入的是数字,还是猜对了数字?


if (playerGuess == number) {
  if (playerGuess == number && tryCount < (2)) {
    ...

你这里有双重条件。第二个条件只有在第一个条件已经为真时才会运行。因此第二个

playerGuess == number
是完全多余的。

此外,

(2)
中的括号不执行任何操作。你可以只写
2
.


} else if (number > playerGuess && tryCount < (9)) {

整个条件都在

if
内,询问玩家是否已经猜对了数字。在代码的这一点上,猜到的数字总是正确的,所以这个条件总是假的。你可能想把这个和所有其他
else
块放在外面的
if 
块中检查数字是否太高或太低。

© www.soinside.com 2019 - 2024. All rights reserved.