【问题标题】:Guess the number game in java在java中猜数字游戏
【发布时间】:2014-03-16 01:35:32
【问题描述】:
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 或小于零的猜测,我的猜测也太高了。任何帮助是极大的赞赏。提前致谢。

【问题讨论】:

  • 我不认为如果您输入的猜测大于 100 或小于 0,您的 while 循环将是错误的,并且只会打印最后一行“您猜到了其中的数字”

标签: java while-loop


【解决方案1】:

这个循环:

// 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);
}

我还没有执行,所以它可能包含一些小问题。

【讨论】:

  • 请让我知道您的想法以及如何根据密码检查它。只要猜测不正确,这个循环就会一直持续: while (game.getHighest() -guess > guess ||guess > game.getHighest()) {guess++; if (guess guess - game.getHighest()){ System.out.println("你猜的太低了!"); } 扫描仪键盘 = new Scanner(System.in);字符串纯文本 = 键盘.nextLine(); }转++;
  • 首先,isGuessHigher 方法必须检查 secret 而不是 highest,因为您猜测的是秘密值,而不是它的上限。其次,您不需要对用户的猜测进行任何算术运算,只需使用isGuessHigher 方法检查它们,在屏幕上显示结果(高于、等于或低于秘密),如果不是则继续游戏等于秘密。
  • 我如何检查密码,我的修改是否在正确的轨道上
  • 更新了答案,替换了您的 main 方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 1970-01-01
  • 2016-06-21
  • 2013-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多