gpt4 book ai didi

javascript - do while javascript - 猜谜游戏循环

转载 作者:搜寻专家 更新时间:2023-11-01 05:30:20 25 4
gpt4 key购买 nike

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>Game</title>
<script type="text/javascript" src="game.js"></script>

</head>
<body>
<h1>GameSite</h1>
<p> This program will generate a random number. Please input your guess in the box!</p>
<button onclick="guessGame()">Press me to play a quick game!</button>

<script>
function guessGame(){
number = Math.floor(Math.random()*11);
document.write(number);
var guess = prompt("Guess a number: ");
do {
guess = prompt("Keep guessing!");
if (number < guess) {
prompt("You've guessed too high!");
} else if (number > guess) {
prompt("You've guessed too low!");
} else document.write("Good Job!");
} while (guess != number);
}
</script>
</body>
</html>

我在使用循环时遇到问题。它循环正常,但如果我在 if 语句之后也移动 guess 语句,它每次都会回到那个位置。如果我把它放在 if 语句之外,那么它似乎实际上不允许我猜测一个有效数字。很难解释:\

最佳答案

我建议只使用一个提示并将其分配给变量。

function guessGame() {
var number = Math.random() * 11 | 0,
guess,
text = 'Guess a number:';
do {
guess = prompt(text);
if (number < guess) {
text = "You've guessed too high!";
} else if (number > guess) {
text = "You've guessed too low!";
}
} while (guess != number);
document.write("Good Job!");
}
guessGame();

关于javascript - do while javascript - 猜谜游戏循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31741269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com