- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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);
}
}
大家好>我正在用java编写一个猜测 secret 数字的程序,但遇到了一些问题。当我第一次运行该程序时,我不断发现我的猜测太高,而在我终止程序并再次运行它后,它说我的猜测太低。在 while 循环中,只要猜测是错误的数字,它就会循环,我相信它正在这样做,但我显然做错了一些事情。感谢任何帮助,提前致谢。
最佳答案
您的循环检查猜测
的正确性。如果 guess
不正确,您可以从命令行读取一个新值并将其分配给 GuessTheNumber
,而不是分配给 guess
。
我也不明白你为什么要进行guess++
。这没有意义。
关于java - 猜猜 secret 数字java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22440277/
我正在设置我的 Authlogic 用户 session 功能。我有点困惑。当我运行时: cucumber features 我收到一些红色错误 Scenario: User signs i
我正在开发 Guess Who?游戏使用 Prolog。游戏的机制非常简单。一个玩家(在这种情况下,人类)从许多可能的人中选择一个人,另一个玩家(计算机)开始询问关于这个人的某些属性的是/否问题。最终
package edu.blastermind.model; import java.util.Random; /** * A NumberGuessingGame represents the r
自从更新到 Android Studio 3.2.0 我面临以下问题: Execution failed for task ':mobile:dataBindingGenBaseClassesDebu
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是一名优秀的程序员,十分优秀!