不在JS数字游戏中显示输出。

huangapple go评论73阅读模式
英文:

Not displaying output in JS number game

问题

以下是您代码的翻译部分:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随机数游戏</title>
</head>
<body>
    <div>
        <h1>猜一个随机数!</h1>
        <input type="text" id="input" placeholder="在此输入猜测!"><br>
        <button onclick="displayOutput()">提交</button>
        <p>在上方的空格中输入1到100之间的数字,看看你是否猜对了。</p>
        <span id="output"></span>
    </div>

    <script>
        function displayOutput() {
            let inputValue = document.getElementById("input").value;
            let randomNumber = Math.floor(Math.random() * 100) + 1;

            if (inputValue == null || inputValue == "") {
                document.getElementById("output").innerHTML = "你必须输入1到100之间的值。";
            } else if (inputValue != randomNumber) {
                document.getElementById("output").innerHTML = "你的猜测不正确。再试一次。";
            } else {
                document.getElementById("output").innerHTML = "你猜对了!你的数字是 " + inputValue + ",做得好。";
            }
        }
    </script>
</body>
</html>

请注意,我已经修复了代码中的几个错误,包括随机数生成方式和条件语句的语法。希望这可以帮助您让您的随机数游戏正常运行。如果您还有其他问题,请随时提出。

英文:

I'm a student and I'm doing an assignment for my coding class which involves me making a random number game that asks the user for input and then display the output. So far my code looks like this. We just started conditionals and I'm unsure about a couple of things.

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Random Number Game&lt;/title&gt;

    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div&gt;
        &lt;h1&gt;Guess a random number!&lt;/h1&gt;
        &lt;input type=&quot;text&quot; id=&quot;input&quot; placeholder=&quot;Type guess here!&quot;&gt;&lt;br&gt;
        &lt;button onclick=&quot;displayOutput()&quot;&gt;Submit&lt;/button&gt;
        &lt;p&gt;Type in a number 1 - 100 in the space above and see if you were right or not.&lt;/p&gt;
        &lt;span id=&quot;output&quot;&gt;&lt;/span&gt;
    &lt;/div&gt;

    &lt;script&gt;

        function displayOutput() {
            let inputValue = document.getElementById(&quot;input&quot;).value;
            let randomNumber = Math.floor(Math.random() * 1, 101);

            if (inputValue == null || inputValue == &quot;&quot;) {
                document.getElementById(&quot;output&quot;).innerHTML = 
                    &quot;You have to enter a value between 1 and 100&quot;;
            } else if (inputValue != randomNumber) {
                document.getElementById(&quot;output&quot;).innerHTML = 
                    &quot;Your guess was incorrect. Try again.&quot;;
            } else (inputValue == randomNumber) {
                document.getElementById(&quot;output&quot;).innerHTML = 
                    &quot;You guessed correctly! Your number was &quot; + inputValue &quot;Good job.&quot;;
            }

        }
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

I put a number in and was obviously, expecting it to display and it didn't. I thought maybe I got some names wrong or messed up in the conditionals. Can someone tell me what I did wrong?

答案1

得分: 1

你的代码中存在一些小问题。

  1. 首先,你添加了 &lt;/style&gt;,这没有任何意义。我只是将其删除了。

  2. else 语句不能带有条件,如果你想添加更多条件,应该使用 else if (条件),但 else 是最后一个,不带条件的。

这是经过小改动的你的代码。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Random Number Game</title>
</head>
<body>
    <div>
        <h1>猜一个随机数!</h1>
        <input type="text" id="input" placeholder="在这里输入猜测!"><br>
        <button onclick="displayOutput()">提交</button>
        <p>在上面的空格中输入1 - 100之间的数字,看看你是否猜对了。</p>
        <span id="output"></span>
    </div>

    <script>
        function displayOutput() {
            let inputValue = document.getElementById("input").value;
            let randomNumber = Math.floor(Math.random() * 100) + 1;

            if (inputValue == null || inputValue == "") {
                document.getElementById("output").innerHTML =
                    "你必须输入一个1到100之间的值";
            } else if (inputValue != randomNumber) {
                document.getElementById("output").innerHTML =
                    "你的猜测不正确,请再试一次。";
            } else {
                document.getElementById("output").innerHTML =
                    "你猜对了!你的数字是 " + inputValue + ",做得好。";
            }
        }
    </script>
</body>
</html>
英文:

There were small issues in your code.

  1. First of all you added &lt;/style&gt; that isn't making any sense. I simply removed that.

  2. else statement cannot have a condition if you want to add more conditions you should use else if (condition) but else is the last one with no condition.

Here, is your code with little changes.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Random Number Game&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div&gt;
        &lt;h1&gt;Guess a random number!&lt;/h1&gt;
        &lt;input type=&quot;text&quot; id=&quot;input&quot; placeholder=&quot;Type guess here!&quot;&gt;&lt;br&gt;
        &lt;button onclick=&quot;displayOutput()&quot;&gt;Submit&lt;/button&gt;
        &lt;p&gt;Type in a number 1 - 100 in the space above and see if you were right or not.&lt;/p&gt;
        &lt;span id=&quot;output&quot;&gt;&lt;/span&gt;
    &lt;/div&gt;

    &lt;script&gt;

        function displayOutput() {
            let inputValue = document.getElementById(&quot;input&quot;).value;
            let randomNumber = Math.floor(Math.random() *1,101);

            if (inputValue == null || inputValue == &quot;&quot;) {
                document.getElementById(&quot;output&quot;).innerHTML = 
                    &quot;You have to enter a value between 1 and 100&quot;;
            } else if (inputValue != randomNumber) {
                document.getElementById(&quot;output&quot;).innerHTML = 
                    &quot;Your guess was incorrect. Try again.&quot;;
            } else{
                document.getElementById(&quot;output&quot;).innerHTML = 
                    &quot;You guessed correctly! Your number was &quot; + inputValue+ &quot;Good job.&quot;;
            }

        }
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月8日 20:21:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定