我用JavaScript制作了一个游戏,其中玩家只能猜测1到10的随机数三次,每次程序读取错误的答案时,它将显示应该是正确的答案,并告诉玩家再试一次,如果玩家得到正确答案,游戏会显示一条消息,提示您您理解正确。我设法使程序正常工作,显然,一切似乎都很好,除了一件事,即使玩家得到正确的答案,程序也不会停止,它会读取所有语句,直到结束。 如何在正确答案后停止?
var random = Math.floor(Math.random() * 10);
var random1 = Math.floor(Math.random() * 10);
var random2 = Math.floor(Math.random() * 10);
var answer = window.prompt("Make a guess from 1 to 10, you have 3 chances.");
if (answer == random) {
alert("HORAAYYYY YOU GOT IT RIGHT!!!");
} else {
alert("Sorry, the correct answer was " + random);
window.prompt("Make a guess from 1 to 10, you have 2 chances left.");
}
if (answer == random1) {
alert("HORAAYYYY YOU GOT IT RIGHT!!!");
} else {
alert("Sorry, the correct answer was " + random1);
window.prompt("Make a guess from 1 to 10, you have 1 chance left.");
}
if (answer == random2) {
alert("HORAAYYYY YOU GOT IT RIGHT!!!");
} else {
alert("Sorry, the correct answer was " + random2);
alert("You've lost");
}
答案 0 :(得分:3)
您的代码中有错误。如果您在第一轮获胜,它将成功检测到这一点。在第二轮和第三轮中,任何胜利都是纯粹的巧合。
原因是因为您没有在每次调用时都将返回值从window.prompt
分配给answer
。因此answer
的值永远不会从一个回合更改为另一个回合。
要解决此问题,您应该替换
window.prompt(...)
具有:
answer = window.prompt(...)
让我们从一个非常简单(但非常 不好 )的解决方案开始,并将其用作教授更好的架构设计的跳板。
您当前的代码大致如下:
if (win) {
// say you won
} else {
// say you lost
}
if (win) {
// say you won
} else {
// say you lost
}
if (win) {
// say you won
} else {
// say you lost
}
在清理完所有多余的内容之后,您可以清楚地看到它为什么要经历所有三个迭代:三个if / else块完全无关,彼此一无所知。每次它依次运行一个if / else块,然后运行另一个,然后运行另一个。
最简单的解决方法是确保以后的阻止仅在您丢失时运行。这很容易做到,因为我们已经知道您是否输了-当您不赢时就会发生!
if (win) {
// say you won
} else {
// say you lost
if (win) {
// say you won
} else {
// say you lost
if (win) {
// say you won
} else {
// say you lost
}
}
}
或者,使用您的random
,random1
和random2
变量:
var random = Math.floor(Math.random() * 10);
var random1 = Math.floor(Math.random() * 10);
var random2 = Math.floor(Math.random() * 10);
var answer = window.prompt("Make a guess from 1 to 10, you have 3 chances.");
if (answer == random) {
alert("HORAAYYYY YOU GOT IT RIGHT!!!");
}
else {
alert("Sorry, the correct answer was " + random);
answer = window.prompt("Make a guess from 1 to 10, you have 2 chances left.");
if (answer == random1) {
alert("HORAAYYYY YOU GOT IT RIGHT!!!");
}
else {
alert("Sorry, the correct answer was " + random1);
answer = window.prompt("Make a guess from 1 to 10, you have 1 chance left.");
if (answer == random2) {
alert("HORAAYYYY YOU GOT IT RIGHT!!!");
}
else {
alert("Sorry, the correct answer was " + random2);
alert("You've lost");
}
}
}
这很丑,但是可以用。
如您所想,向该游戏添加4或5或6回合将非常繁琐。您必须输入更多个if / else块,创建更多个随机变量,然后输入更多个警报语句。更令人讨厌的是,所有这些警报语句都包含相同的文本!
软件设计中有一个名为DRY的概念(请不要重复自己)。这意味着,如果您有两行相同的代码,则可以重写它以消除重复。
对于您而言,我们可以使用while
循环来执行此操作,以检查我们是否赢得了比赛:
var youHaveWon = false;
while( ! youHaveWon ) {
var random = Math.floor(Math.random() * 10);
var answer = window.prompt("Make a guess from 1 to 10");
if (answer == random) {
alert("HORAAYYYY YOU GOT IT RIGHT!!!");
youHaveWon = true;
}
else {
alert("Sorry, the correct answer was " + random);
}
}
这将使您不断猜测,直到正确为止,并且不重复任何代码。尽管这没有将您限制为只有3个猜测。为此,我们应该再引入一个变量:
var youHaveWon = false;
var guessesRemaining = 3;
while( ! youHaveWon && guessesRemaining > 0 ) {
var random = Math.floor(Math.random() * 10);
var answer = window.prompt("Make a guess from 1 to 10... you have " + guessesRemaining + " more guesses");
if (answer == random) {
alert("HORAAYYYY YOU GOT IT RIGHT!!!");
youHaveWon = true;
}
else {
alert("Sorry, the correct answer was " + random);
}
guessesRemaining = guessesRemaining - 1;
}
if ( ! youHaveWon ) {
alert("You lost");
}
答案 1 :(得分:1)
您可以将其包装到函数中并以正确的值返回。返回后,JS将停止执行,控件将向前移动
尝试以下
function findNumber() {
var random = Math.floor(Math.random() * 10);
var random1 = Math.floor(Math.random() * 10);
var random2 = Math.floor(Math.random() * 10);
var answer = window.prompt("Make a guess from 1 to 10, you have 3 chances.");
if (answer === random) {
alert("HORAAYYYY YOU GOT IT RIGHT!!!");
return true;
} else {
alert("Sorry, the correct answer was " + random);
window.prompt("Make a guess from 1 to 10, you have 2 chances left.");
}
if (answer == random1) {
alert("HORAAYYYY YOU GOT IT RIGHT!!!");
return true;
} else {
alert("Sorry, the correct answer was " + random1);
window.prompt("Make a guess from 1 to 10, you have 1 chance left.");
}
if (answer == random2) {
alert("HORAAYYYY YOU GOT IT RIGHT!!!");
return true;
} else {
alert("Sorry, the correct answer was " + random2);
alert("You've lost");
}
}
答案 2 :(得分:1)
问题是您拥有三个不同的if块,即使用户可能已经猜出了正确的数字,它们也会全部执行。 我建议设置一个随机数和一个全局计数器,以跟踪剩余机会。
function validate() {
if (answer == random) {
alert("HORAAYYYY YOU GOT IT RIGHT!!!");
} else {
if (chances - 1 > 0) {
chances--;
answer = window.prompt("Make a guess from 1 to 10, you have " + chances + " chances.");
validate();
}
}
}
var random = Math.floor(Math.random() * 10);
var chances = 3;
var answer = window.prompt("Make a guess from 1 to 10, you have " + chances + " chances.");
validate();
答案 3 :(得分:1)
var randomNumbers = [];
var numberOfTries = 3;
for(var i=0; i<numberOfTries ; i++){
randomNumbers.push(Math.floor(Math.random() * 10));
}
for(var i=numberOfTries-1; i > -1 ; i--){
var answer = window.prompt("Make a guess from 1 to 10, you have "+ parseInt(i + 1) +" chances left.");
if(answer === randomNumbers[i]) {
alert("HORAAYYYY YOU GOT IT RIGHT!!!");
return;
}
else {
alert("Sorry, the correct answer was " + randomNumbers[i]);
}
}