猜测游戏,1到100使用do / while循环和if / ese如果条件语句

时间:2016-06-13 17:01:02

标签: javascript if-statement do-while

我正在尝试用简单的java脚本制作一个猜谜游戏。教授给了我们一个指南,但我仍然有点困惑。根据指南,我们将使用do / while循环,并在该循环中,使用if / else if条件语句,这是我到目前为止所拥有的。 我已经尝试过多次改变它,而我最终得到的是一个永无止境的循环,即使在数字被猜到之后,它也没有停止

<body>
    <p>
        I'm thinking of a number between 1 and 100, try to guess it! </br>

        <script>
            var number = Math.floor(Math.random() * 100 + 1)
        //guess variable to store the guessed number by user
            var guess 
        //output to store output to the user
            var output 
        //if the user guessed the number or not, initialize it to false
            var guessed = false

        //do/while loop, while condition is based on if the user NOT guessing the number (e.g. guessed == false)
        do {
            guess = prompt ("Think of a number between 1 and 100, what is your number?");
            document.write ("You guessed the number " + guess + "<br/>");
            if (guess > number) {
                document.write ("You guessed too high, think smaller" + "<br/>");
                guessed = false
                }
            else if (guess < number){
                document.write ("You guessed too low, think bigger" + "<br/");
                guessed = false
                }
            else {
                alert("You guessed the right number!")
                guessed = true}
        }
        while (guessed = false) 


        </script>
    </p>
</body>

4 个答案:

答案 0 :(得分:0)

我相信你在这里错过了一个分号:

while(guessed == false);

这只是do-while循环特有的要求,并不会出现在其他循环的语法中。

答案 1 :(得分:0)

您的代码存在一些问题。

首先,您需要将while (guessed = false)更改为while (guessed === false),因为它应该是比较,因此您必须使用比较器。

其次,您正在使用document.write()。这不起作用,因为HTML在用户中断循环之前不会改变。最好使用alert()代替。{1}}。这是一个有效的例子:

var number = Math.floor(Math.random() * 100 + 1)
  //guess variable to store the guessed number by user
var guess
  //output to store output to the user
var output
  //if the user guessed the number or not, initialize it to false
var guessed = false

//do/while loop, while condition is based on if the user NOT guessing the number (e.g. guessed == false)
do {
  guess = prompt("Think of a number between 1 and 100, what is your number?");
  if (guess > number) {
    alert("You guessed too high, think smaller");
    guessed = false
  } else if (guess < number) {
    alert("You guessed too low, think bigger");
    guessed = false
  } else {
    alert("You guessed the right number!")
    guessed = true
  }
}
while (guessed === false)

答案 2 :(得分:0)

在你的情况下(猜测=假)你使用一个相等的signe(=),女巫永远是真的。你必须写(猜猜==假)或(猜猜= =假)。为了尝试我会这样做:

             我想的是1到100之间的数字,试着去猜测!     

<script type="text/javascript">
    var number = Math.floor(Math.random() * 100 + 1);
    // Just for Test
    document.getElementById('test').innerHTML += '<br />' + number;
    //guess variable to store the guessed number by user
    var guess;
    //output to store output to the user
    var output;
    //if the user guessed the number or not, initialize it to false
    var guessed = false;

    //do/while loop, while condition is based on if the user NOT guessing the number (e.g. guessed == false)
    do {
        guess = prompt("Think of a number between 1 and 100, what is your number?");
        document.getElementById('test').innerHTML += '<br />' + "You guessed the number " + guess;
        if (guess > number) {
            document.getElementById('test').innerHTML += '<br />' + "You guessed too high, think smaller";
            guessed = false;
        }
        else if (guess < number) {
            document.getElementById('test').innerHTML += '<br />' + "You guessed too low, think bigger";
            guessed = false;
        }
        else {
            alert("You guessed the right number!")
            guessed = true;
        }
    }
    while (guessed == false)
</script>

答案 3 :(得分:0)

你错过了大量的冒号,虽然你的代码仍然可以在没有它们的情况下运行。就像其他人都说你在guessed结束时将false分配给do while loop,因此它永远不会爆发。要检查一个条件,你应该使用3个等号(这是严格的比较)。有关here的更多信息。如果应该使用document.write,这是值得商榷的。可以找到原因here。此外,在测试代码时它会间歇性地工作,所以我添加了一个带有id提示的div。然后我使用var elHints = document.getElementById('#hints');抓住了该元素然后将内容添加到element提示,只需执行elHints.textContent += ...。加等于(+=)会向元素添加内容而不会覆盖现有内容。在css中,我向#hints添加了一种样式:white-space: pre-line;,当使用\n添加内容时,这允许换行符divtextContent。更多关于here

以下是完整的JavaScript代码:

var number = Math.floor(Math.random() * 100 + 1);
//guess variable to store the guessed number by user
var guess;
//output to store output to the user
var output;
//if the user guessed the number or not, initialize it to false
var guessed = false;
var elHints = document.getElementById('hints');

//do/while loop, while condition is based on if the user NOT guessing the number (e.g. guessed == false)
do {
  guess = prompt("Think of a number between 1 and 100, what is your number?");
  elHints.textContent += "You guessed the number " + guess + '\n';
  if (guess > number) {
    elHints.textContent += "You guessed too high, think smaller" + '\n';
    guessed = false;
  } else if (guess < number) {
    elHints.textContent += "You guessed too low, think bigger" + '\n';
    guessed = false;
  } else {
    alert("You guessed the right number!")
    guessed = true;
  }
}
while (guessed === false);

这是jsfiddle