该应用程序是一个数字猜测游戏,我的问题是当用户获得随机数时,它会询问用户是否希望继续,如果他键入“y”它会创建一个新的随机数并要求用户猜测它。它然后应该要求用户“输入一个数字”。相反,我得到无法从代码中删除一行?
“过高”(或 “过低”)
“输入号码”
EX输出:
输入号码:
更正你得到它!号码是2 你有2次尝试。
你想再次玩(y/n):
y
太低!再试一次。
输入号码:
我该如何解决问题而不打印输入数字后确定的“太高”或“太低”?
ps。我试了很多办法,但我坚持:(
public static void main(String[] args) {
System.out
.println("Welcome to the gussing game, Try to guess the number am thinking of to win!");
System.out
.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println();
System.out
.println("Am thinking of a number between 0 and 10. Try to guess it.");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
double rightNum = Math.random() * 10;
int randomNum = (int) rightNum; // convert the random number to int
int tries = 0;
while (!choice.equalsIgnoreCase("n")) {
System.out.println("Enter the Number:");
int guess = sc.nextInt();
tries++;
if (guess == randomNum) {
System.out.println("Correct you've got it! The Number Was "
+ randomNum);
System.out.println("You got it in " + tries + " tries.");
System.out.println("Would you like to play again (y/n):");
choice = sc.next();
if (choice.equalsIgnoreCase("y"))
// reset the random number
{
rightNum = Math.random() * 10;
randomNum = (int) rightNum;
tries = 0;
}
}
if (guess > randomNum + 10) {
System.out.println("Way to high! Try again.");
} else if (guess < randomNum) {
System.out.println("Too low! Try again.");
} else if (guess > randomNum && guess <= randomNum + 10) {
System.out.println("Too high! Try again.");
}
}
}
}
来吧!花5分钟自行解决这个问题,你将学到的不仅仅是从这里得到答案。你怎么样学习如何使用调试器进入你的代码?然后你会看到你的程序出错了,这很明显。 – Mic 2013-02-20 03:41:12