【发布时间】:2021-01-17 00:44:13
【问题描述】:
我正在尝试制作这个迷你猜谜游戏。用户必须猜数字。 3 次尝试后,系统会询问用户是否要继续。我已经编写了逻辑来提出 3 个问题,并提示用户决定是继续还是终止程序。但是我正在努力做到这一点,如果用户继续玩,他会继续玩。
我在想一个介于 1 和 10 之间的数字,它是什么?> 3 10 你的猜测太低了 我在想一个介于 1 和 10 之间的数字,它是什么?> 2 10 你的猜测太低了 我在想一个介于 1 到 10 之间的数字,它是什么?> 5 6 你的猜测太低了 您要继续是还是否?(y 表示是,n 表示否)> y 我在想一个介于 1 和 10 之间的数字,它是什么?> 3 1 你的猜测太高了 你要继续yes还是no?(y代表yes,n代表no)>
是或否问题不应该出现在第三个问号之前。任何人都有任何想法或知道应该使用什么循环来朝着正确的方向前进
这些是上面的结果:
function game() {
let numOfGuesses=0;
let inputNum;
while(numOfGuesses <= 10) {
var userGuess = prompt("I'm thinking of a number between 1 and 10, what is it?");
var randomNum = Math.floor(Math.random() * 10) + 1;
let number = parseInt(userGuess);
numOfGuesses++;
console.log(randomNum)
var quit;
//logic to check which are correct and not correct
if (userGuess < randomNum) {
alert("your guess is too low")
}else if(userGuess > randomNum) {
alert("your guess is too high")
}else {
alert("CORRECT!")
}
if (numOfGuesses >= 3 || numOfGuesses == 3) {
quit = prompt("would you like to continue yes or no?(y for yes, n for no)")
//break;
if(quit == "y") {
continue;
} else {
return;
}
}
}
}
game();
【问题讨论】:
标签: javascript loops while-loop