这里是新程序员-我正在尝试创建一个猜数字游戏。 如果我猜得太高,它会告诉我“太高,请再猜一次”。 如果我猜得太低,它会告诉我“太低,请再猜一次”。 如果我猜对了,那就告诉我正确。 但是我只能在每个方向上猜出1个错误的答案。 我已经搜索并搜索并尝试了几乎所有内容(如果/其他/同时)
我知道这可能是有经验的程序员的简单解决方案。 请帮忙。 谢谢---
var secretNumber = 53;
var guess = prompt('Guess a number');
if (guess == (53)) {
alert("Yes you got it! 53 is the right answer!")}
while (guess < (53)) {
var guess = prompt('Too Low, Guess again');
}
while (guess > (53)) {
var guess = prompt('Too High, Guess again');
}
答案 0 :(得分:0)
尝试一下:
var secretNumber = 53;
var high = false;
var low = false;
var done = false;
var guess = prompt('Guess a number');
while (!done) {
if (guess === secretNumber) {
alert("Yes you got it! " + secretNumber + " is the right answer!");
done = true;
} else if (guess < secretNumber) {
if (low) {
alert('You have failed');
done = true;
} else {
low = true;
guess = prompt('Too Low, Guess again');
}
} else if (guess > secretNumber) {
if (high) {
alert('You have failed');
done = true;
} else {
high = true;
guess = prompt('Too High, Guess again');
}
}
}
这不是最佳选择,但对于本练习应该如此。