如何在猜数字游戏中限制猜测次数?

huangapple go评论87阅读模式
英文:

How do I add a limit to the number of guesses in a number guessing game?

问题

我无法弄清楚如何在我的数字猜谜游戏中添加猜测次数的限制。我尝试在while语句后面添加了一个for语句,但代码只会在第十次猜测时停止,而且永远没有赢家。我删除了while语句,只使用了for语句,这确保用户在第九次猜测时得到正确的答案。我的代码按照我的教授的要求分成了两个类。我在下面包含了这两个类。我会非常感谢您能提供的所有帮助。谢谢!

GuessingGame.java:主类

public class GuessingGame {
    public static void main(String[] args) {
        new Guess().doGuess();
    }
}

Guess.java:

class Guess {
    private int answer = 0;
    int tries = 0;
    Scanner input = new Scanner(System.in);
    int guess, i;
    boolean win = false;
    int amount = 10;
    
    public Guess() {
        answer = generateRandomNumber();
    }

    // 生成一个1到一千之间的私有数字
    private int generateRandomNumber() {
        Random rand = new Random();
        return rand.nextInt(1000) + 1;
    }

    public void doGuess() {
        while(!win) {
            System.out.println("你只有十次尝试机会。" +
                    "猜一个1到1000之间的数字:");
            guess = input.nextInt();
            if (guess > 1000) {
                System.out.println("你猜的数字超出范围!");
            } else if (guess < 1) {
                System.out.println("你猜的数字超出范围!");
            } else if (guess == answer) {
                win = true;
                tries++;
            } else if (guess < answer && i != amount - 1) {
                System.out.println("你猜的数字太低!");
                tries++;
            } else if (guess > answer && i != amount - 1) {
                System.out.println("你猜的数字太高!");
                tries++;
            }
        }
        System.out.println("恭喜!你猜对了数字!" +
                "数字是:" + answer);
        System.out.println("你用了 " + tries + " 次尝试");
    }
}
英文:

I cannot figure out how to add a limit to the number of guesses in my number guessing game. I have tried adding a for statement after the while statement but that made the code just stop at the tenth guess and there was no winner ever. I deleted the while statement and just did the for statement which ensured that the user got the correct answer every ninth guess. My code is divided into two classes as requested by my professor. I am including both below. I would appreciate all the help I can get. Thank you!

GuessingGame.java: Main Class

public class GuessingGame {
    public static void main(String[] args) {
        new Guess().doGuess();
    }
}

Guess.java

class Guess {
private int answer = 0;
int tries = 0;
Scanner input = new Scanner(System.in);
int guess, i;
boolean win = false;
int amount = 10;
public Guess() {
answer = generateRandomNumber();
}
//Generate a private number between 1 and a thousand
private int generateRandomNumber() {
Random rand = new Random();
return rand.nextInt(1000) + 1;
}
public void doGuess() {
while(!win) {
System.out.println(&quot;You are limited to ten attempts.&quot;
+ &quot;Guess a number between 1 and 1000: &quot;);
guess = input.nextInt();
if (guess &gt; 1000 ) {
System.out.println(&quot;Your guess is out of the range!&quot;);
} else if (guess &lt; 1) {
System.out.println(&quot;Your guess is out of the range!&quot;);
} else if (guess == answer) {
win = true;
tries++;
} else if (guess &lt; answer &amp;&amp; i != amount -1) {
System.out.println(&quot;Your guess is too low!&quot;);
tries++;
} else if (guess &gt; answer &amp;&amp; i != amount -1) {
System.out.println(&quot;Your guess is too high!&quot;);
tries++;
}
}
System.out.println(&quot;Congragulations! You guessed the number!&quot;
+ &quot;The number was: &quot; +answer);
System.out.println(&quot;It took you &quot; + tries + &quot; tries&quot;);
}
}

答案1

得分: 0

你可以在while循环内部添加一个if语句。

 public void doGuess() {
           while(!win) {
            System.out.println("您最多有十次尝试。"
                    + "猜一个介于1和1000之间的数字:");
            guess = input.nextInt();
            if(tries > 9) {
              ...无论当用户达到10次猜测时您想要发生什么...
            }
英文:

You can add an if-statement inside the while loop.

 public void doGuess() {
while(!win) {
System.out.println(&quot;You are limited to ten attempts.&quot;
+ &quot;Guess a number between 1 and 1000: &quot;);
guess = input.nextInt();
if(tries &gt; 9) {
...whatever you want to happen when user has reached 10 guesses...
}
</details>

huangapple
  • 本文由 发表于 2020年9月21日 12:13:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63986115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定