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.westga.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 (guess <= game.getHighest() && guess >= 0) {
            guess++;
        if (guess < game.getHighest()){
            System.out.println("You guessed too high!");
        }
            else if (guess > game.getHighest()){
                System.out.println("You guessed too low!");
            }
            Scanner keyboard = new Scanner(System.in);
            String plainText = keyboard.nextLine();
        }
            turns++;

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

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


大家好,我正在编写一个猜猜数字游戏程序,并且我的非法参数异常无法正常工作,这是说即使我输入的猜想大于100或小于零,我的猜想也仍然很高。任何帮助是极大的赞赏。提前致谢。

最佳答案

这个循环:

// TODO 4: loop as long as the guess is not correct
while (guess <= game.getHighest() && guess >= 0) {
    guess++;


没有按照您在评论中的陈述进行操作。这将使您的guess变量始终达到highest值。不检查secret号。

更新1

将此替换为main方法:

public static void main(String[] args) {

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

    do {
        Scanner kb = new Scanner(System.in);

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

        if (game.isGuessHigher(guess)){
            System.out.println("You guessed too high!");
            turns++;
        } else if (!game.isGuessCorrect(guess)){
            System.out.println("You guessed too low!");
            turns++;
        }
    } while (!game.isGuessCorrect(guess));
    System.out.printf("You guessed the number in %d turns\n", turns);
}


我没有执行它,因此它可能包含一些小问题。

10-04 19:07