如何询问用户是否想再次玩游戏?

时间:2014-03-25 01:03:44

标签: javascript loops while-loop do-while

我想询问用户他们是否只想在正确猜测的情况下再次玩游戏。如果他们说不,我想结束该计划。所以基本上是 - >再玩一次,没有 - >结束程序

<html>
<head>
<title>NumberGuesser</title>
<script>

var guess = 99;
var target = 0;
var turns = 0;
var msg = "";

target = Math.floor(Math.random() * 100) + 1;
alert (target);

msg = "Guess my number between 1 and 100 !. \n";
msg += "Guess the number, and I'll tell you if it's too high, \n";
msg += "too low, or correct. ";
alert (msg);

guess = eval(prompt("What is your guess?", ""));

while ( guess != target){
  if (guess > target){
guess = prompt (turns + ".  Too high!! Try again!!", "");
  } // end if

  if (guess < target){
    guess = prompt (turns + ".  Too low!! Try again!!", "");
  } // end if

  if (guess == target){
    msg = "YOU WIN!!! \n";
    alert (msg);
  } // end if
} // end while

</script>
</head>

<body>
<center><center>
<h1>NumberGuesser<br></h1>
</center>
<hr>
</body>
</html>

谢谢大家,我在javascript中度过了一段艰难的时光。

2 个答案:

答案 0 :(得分:0)

这样做:

<html>
<head>
<title>NumberGuesser</title>
<script>

var guess = 99;
var target = 0;
var turns = 0;
var msg = "";
var win = false;

target = Math.floor(Math.random() * 100) + 1;
alert (target);

msg = "Guess my number between 1 and 100 !. \n";
msg += "Guess the number, and I'll tell you if it's too high, \n";
msg += "too low, or correct. ";
alert (msg);

guess = eval(prompt("What is your guess?", ""));

while ( guess != target){
if (guess > target){
guess = prompt (turns + ".  Too high!! Try again!!", "");
} // end if

if (guess < target){
guess = prompt (turns + ".  Too low!! Try again!!", "");
} // end if

if (guess == target){
msg = "YOU WIN!!! \n";
alert (msg);
win = true;
} // end if
if(win = true) {
alert("Play again?");
}
} // end while

</script>
</head>

<body>
<center><center>
<h1>NumberGuesser<br></h1>
</center>
<hr>
</body>
</html>

它真的是相同的代码,只是我添加了一些额外的东西。显然,您需要编辑我添加的警报,但是一旦您更改它以接受正确的输入,您的所有设置。

答案 1 :(得分:0)

你想要再次执行整个程序,直到他们说停止这样做,所以你需要将while循环放在整个程序中(最后用户提示)。

var guess = 99;
var target = 0;
var turns = 0;
var msg = "";

var keep_looping = true;

// this is the new loop. 
// The whole program is now inside this loop, so the whole program will 
// be repeated over and over until "keep_looping" is false

while (keep_looping) {    



   target = Math.floor(Math.random() * 100) + 1;
    alert (target);

    msg = "Guess my number between 1 and 100 !. \n";
    msg += "Guess the number, and I'll tell you if it's too high, \n";
    msg += "too low, or correct. ";
    alert (msg);

    guess = eval(prompt("What is your guess?", ""));

    while ( guess != target){
      if (guess > target){
    guess = prompt (turns + ".  Too high!! Try again!!", "");
      } // end if

      if (guess < target){
        guess = prompt (turns + ".  Too low!! Try again!!", "");
      } // end if

      if (guess == target){
        msg = "YOU WIN!!! \n";
        alert (msg);
      } // end if
    } // end while


  // now we're done with the program this time - we need to deicde if w want to do the loop again...
  // prompt the user if they want to do it again
  // if they reply "no"
  // then change "keep_looping" to false 
}