猜猜秘密号码 包edu.blastermind.model; 导入java.util.Random; /** *NumberGuessingGame代表简单的“猜数字”类型游戏的规则。 * *@作者 * */ 公开课猜数字游戏{ 私人int最高; 私密性; /** *创建一个新的NumberGuessingGame,其密码介于0和最高值之间。 * *@param highest此游戏的最大可能数字。必须大于0。 */ 公众猜测数字游戏(整数最高){ //TODO 1:对参数执行先决条件检查 如果循环检查guess的正确性,并且guess不正确,则从命令行读取一个新值并将其分配给guess编号,而不是分配给guess

猜猜秘密号码 包edu.blastermind.model; 导入java.util.Random; /** *NumberGuessingGame代表简单的“猜数字”类型游戏的规则。 * *@作者 * */ 公开课猜数字游戏{ 私人int最高; 私密性; /** *创建一个新的NumberGuessingGame,其密码介于0和最高值之间。 * *@param highest此游戏的最大可能数字。必须大于0。 */ 公众猜测数字游戏(整数最高){ //TODO 1:对参数执行先决条件检查 如果循环检查guess的正确性,并且guess不正确,则从命令行读取一个新值并将其分配给guess编号,而不是分配给guess,java,if-statement,while-loop,Java,If Statement,While Loop,我也不明白你为什么要做guess++。这没有意义。修复你的主要方法。你遗漏了一点变量,添加了一个来猜测。你总是检查用户的第一次输入 package edu.blastermind.model; import java.util.Random; /** * A NumberGuessingGame represents the rules of a simple "guess the number" type game. * * @author * */ public class

我也不明白你为什么要做
guess++
。这没有意义。

修复你的主要方法。你遗漏了一点变量,添加了一个来猜测。你总是检查用户的第一次输入

package edu.blastermind.model;

import java.util.Random;

/**
 * A NumberGuessingGame represents the rules of a simple "guess the number" type game.
 * 
 * @author 
 *
 */
public class GuessTheNumberGame {

    private int highest;
    private int secret;

    /**
     * Creates a new NumberGuessingGame with a secret number between 0 and highest.
     * 
     * @param highest the highest possible number for this game. Must be > 0.
     */
    public GuessTheNumberGame(int highest) {

        // TODO 1: perform a precondition check on the parameter highest
        if (highest <= 0) {
            throw new IllegalArgumentException
            ("highest number must be at least 1");
        }

        this.highest = highest;

        Random rng = new Random();

        this.secret = rng.nextInt(highest + 1);
    }

    /**
     * Checks to see if we've guessed correctly.
     * 
     * @param guess our guess (must be between 0 and getHighest())
     * @return true if the guess was correct, false otherwise
     */
    public boolean isGuessCorrect(int guess) {

        // TODO 2: perform a precondition check on the variable guess
        if (guess > this.highest || guess < 0) {
            throw new IllegalArgumentException
            ("Guess must be between 1 and 100");
        }

        return this.secret == guess;
    }

    /**
     * Checks to see if our guess is higher than the secret.
     * 
     * @param guess our guess (must be between 0 and getHighest())
     * @return true if our guess is higher than the secret, false otherwise
     */
    public boolean isGuessHigher(int guess) {

        // TODO 3: perform a precondition check on the variable guess
        if (guess > this.highest || guess < 0) {
            throw new IllegalArgumentException
            ("Guess must be between 1 and 100");
        }

        return this.secret < guess;
    }

    /**
     * Returns the highest value in the range of valid guesses.
     * 
     * @return the highest value in the range of valid guesses
     */
    public int getHighest() {
        return this.highest;
    }
}

package edu.blastermind.controllers;

import java.util.Scanner;

import edu.westga.blastermind.model.GuessTheNumberGame;

public class GuessTheNumber {

    public static void main(String[] args) {

        GuessTheNumberGame game = new GuessTheNumberGame(100);
        int turns = 1;

        Scanner kb = new Scanner(System.in);

        System.out.println("Guess a nummber between 0 and 100");
        int guess = kb.nextInt();

        // TODO 4: loop as long as the guess is not correct
        while (!game.isGuessCorrect(guess)) {
            guess++;
        if (game.isGuessHigher(guess)){
            System.out.println("You guessed too high!");
            turns++;
        }
            else if (!game.isGuessCorrect(guess)){
                System.out.println("You guessed too low!");
            }
            int GuessTheNumber = kb.nextInt();
        }
            turns++;

        // TODO 5: in the loop, check guesses and give hints

        System.out.printf("You guessed the number in %d turns\n", turns);
    }
}

你为什么要修改从用户那里得到的猜测?我是怎么做的?请解释
guess++
修改你读到的猜测的值,我相信他是在试图跟踪用户给出的猜测数量。操作:将猜测数量分配给另一个
int
@JB Nizet,我是在增加猜测数量从用户那里,所以我给GuessTheNumber分配了一个新值?你复制了代码吗?你可能添加了一个比需要的多的花括号。我编译并运行了它。很抱歉,这是我的错,我现在正在尝试,看看它是否有效。感谢你的帮助,我没有在while循环后添加花括号。愚蠢的错误
public static void main(String[] args) {

    GuessTheNumberGame game = new GuessTheNumberGame(100);
    int turns = 0;

    Scanner kb = new Scanner(System.in);

    // TODO 4: loop as long as the guess is not correct
    while (true) {
        System.out.println("Guess a nummber between 0 and 100");
        int guess = kb.nextInt();
        if (game.isGuessHigher(guess)) {
            System.out.println("You guessed too high!");
        } else if (!game.isGuessCorrect(guess)) {
            System.out.println("You guessed too low!");
        }
        turns++;
        if (game.isGuessCorrect(guess)) break;
    }

    // TODO 5: in the loop, check guesses and give hints

    System.out.printf("You guessed the number in %d turns\n", turns);
}