在 Javascript 数字猜游戏中插入一个循环



对于作业,我需要做一个JS猜数字游戏。它需要包括一个循环来检查用户的猜测和一个重置游戏按钮。我的问题是让循环工作。我希望匝数从 10 开始。每次用户猜测错误时,他们的回合数都会减少 1,如果他们猜对了,他们的回合数为 0。如果他们按下"开始新游戏"按钮,则应生成一个新数字,并将回合数重置为10。

循环不需要特别需要是一个 while 循环,我只需要在代码中为我的作业提供一个循环。有人可以帮助我吗?

<body>
    <!-- GAME INSTRUCTIONS -->
    <h1>Number Guessing Game</h1>
    <p>A random number between 1 and 100 has been generated. Can you guess it?</p>
    <!-- FORM (Includes button to confirm guess, input box, and output box) -->
    <form id="Input" name="Input">
        <input name="guess" placeholder="Insert your guess" type="number">
        <input name="requestInfo" onclick="getResults()" type="button" value="Confirm">
        <p></p>
        <textarea cols="50" name="results" readonly="true" rows="8"></textarea>
        <p></p><input name="newGame" onclick="resetGame()" type="button" value="Start New Game">
    </form><!-- JAVASCRIPT START -->
    <script type="text/javascript">
// Define variables
var num = Math.floor(Math.random() * 100) + 1;
var turns = 10;
function checkNumber() {
    var guess = parseFloat(document.Input.guess.value);
    while (turns > 0) {
            if (guess == num) {
                turns = 0;
                document.Input.results.value = "Congratulations, you won! The mystery number was " + num + ".";
            } else if (guess < num) {
                turns--;
                document.Input.results.value = "Your guess was too low. Turns remaining: " + turns;
            } else if (guess > num) {
                turns--;
                document.Input.results.value = "Your guess was too high. Turns remaining: " + turns;
            }
    }
}
function resetGame() {
    turns = 10;
    num = Math.floor(Math.random() * 100) + 1;
    document.Input.guess.value = "";
    document.Input.results.value = "";
}
function getResults() {
    checkNumber();
}
    </script>
</body>

好吧,我想既然这是大学/高中作业,你的教授正试图在循环下使用提示来教你。

<body>
<!-- GAME INSTRUCTIONS -->
<h1>Number Guessing Game</h1>
<p>A random number between 1 and 100 has been generated. Can you guess it?</p>
<!-- FORM (Includes button to confirm guess, input box, and output box) -->
<form id="Input" name="Input">
    <input name="requestInfo" onclick="getResults()" type="button" value="Start Guessing!">
    <input name="newGame" onclick="resetGame()" type="button" value="Start New Game">
</form><!-- JAVASCRIPT START -->
<script type="text/javascript">
// Define variables
var num = Math.floor(Math.random() * 100) + 1;
var turns = 10;
function checkNumber() {
while (turns > 0) {
  guess=prompt("Tell me your guess.", "Your guess: ");
        if (guess == num) {
            turns = 0;
            alert("Congratulations, you won! The mystery number was " + num + ".");
        } else if (guess < num) {
            turns--;
            alert("Your guess was too low. Turns remaining: " + turns);
        } else if (guess > num) {
            turns--;
             alert("Your guess was too high. Turns remaining: " + turns);
        }
}
if (turns==0)
alert ("You failed to guess sadly.");
}
function resetGame() {
turns = 10;
num = Math.floor(Math.random() * 100) + 1;
}
function getResults() {
checkNumber();
}
</script>

我同意 taks 似乎有点奇怪 - 显然,使用非模态对话框,您将不需要循环。

您可以做的一件事是使用 prompt 方法(例如:window.prompt("sometext","defaultText"); (,然后打开一个模式对话框来询问用户,直到剩余猜测数为零,或者直到猜测正确。这将在循环中起作用。

这里有一个试试这个。确保用户输入数字。

<body>
    <!-- GAME INSTRUCTIONS -->
    <h1>Number Guessing Game</h1>
    <p>A random number between 1 and 100 has been generated. Can you guess it? Click button to start game.</p>
        <button type="button" onclick="startNewGame()">Start New Game</button>
    <script type="text/javascript">
// Define variables
var num = Math.floor(Math.random() * 100) + 1;
var turns;
function checkNumber() {
    while (turns > 0) {
        var guess = prompt("Insert your guess");
        if (!guess || isNaN(guess)) {
            alert("Please enter a valid number");
            continue;
        }
        if (guess == num) {
            alert("Congratulations, you won! The mystery number was " + num + ".");
            return;
        } else {
            turns--;
            if (guess < num) {
                alert("Your guess was too low. Turns remaining: " + turns);
            } else if (guess > num) {
                alert("Your guess was too high. Turns remaining: " + turns);
            }
        }
    }
    if (turns == 0) {
        alert("You have lost");
    }
}
function startNewGame() {
    turns = 10;
    num = Math.floor(Math.random() * 100) + 1;
    checkNumber();
}
</script>
</body>

相关内容

最新更新