Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何停止JavaScript程序的执行_Javascript_Popup_Alert - Fatal编程技术网

如何停止JavaScript程序的执行

如何停止JavaScript程序的执行,javascript,popup,alert,Javascript,Popup,Alert,作为练习,我用JavaScript做了一个猜测游戏,你必须在三次尝试中猜测随机生成的1到10之间的数字。它工作得很好,但是当三次尝试完成后(或者用户猜到了数字),它又重新开始了。我希望它在满足上述给定条件时停止 代码如下: function runGame() { var isPlaying = true; var num = Math.floor(Math.random() * 10); var guess; var tries = 3; alert("You

作为练习,我用JavaScript做了一个猜测游戏,你必须在三次尝试中猜测随机生成的1到10之间的数字。它工作得很好,但是当三次尝试完成后(或者用户猜到了数字),它又重新开始了。我希望它在满足上述给定条件时停止

代码如下:

function runGame() {

  var isPlaying = true;
  var num = Math.floor(Math.random() * 10);
  var guess;
  var tries = 3;

  alert("You have 3 chances to guess a mindset between 1 and 10!");

    while (tries >= 0) {

    guess = prompt("Enter a guess:");

    if (guess > num) {
      alert("Too high!");
    }

    else if (guess < num) {
      alert("Too low!");
    }  

    else {
      alert("Exactly! " + num + " it is! You've won!");
    }
    tries--;
  }
  
  if (tries == 0) {
    isPlaying = false;
  }
}

while (isPlaying = true) {

  runGame();

}
函数runGame(){
var isplay=true;
var num=Math.floor(Math.random()*10);
var猜测;
var=3;
警惕(“你有3次机会猜测1到10之间的心态!”);
while(尝试次数>=0){
猜测=提示(“输入猜测:”;
如果(猜测>数值){
警惕(“太高了!”);
}
else if(猜测
=在JavaScript中用于为变量赋值。==在JavaScript中,用于比较两个变量

因此,将
isplay=true
更改为
isplay==true
,就可以了

while(trys>=0)
在这里,您可以使用
while(trys>0)

您也可以在函数之外声明这些变量,但这不是必需的

  var isPlaying = true;
  var tries = 3;
function runGame() {
  var num = Math.floor(Math.random() * 10);
  var guess;
  
  alert("You have 3 chances to guess a mindset between 1 and 10!");

    while (tries >= 0) {

    guess = prompt("Enter a guess:");

    if (guess > num) {
      alert("Too high!");
    }

    else if (guess < num) {
      alert("Too low!");
    }  

    else {
      alert("Exactly! " + num + " it is! You've won!");
    }
    tries--;
  }
  
  if (tries == 0) {
    isPlaying = false;
  }
}

while (isPlaying == true) {
  runGame();
}
var isplay=true;
var=3;
函数runGame(){
var num=Math.floor(Math.random()*10);
var猜测;
警惕(“你有3次机会猜测1到10之间的心态!”);
while(尝试次数>=0){
猜测=提示(“输入猜测:”;
如果(猜测>数值){
警惕(“太高了!”);
}
else if(猜测
有几件事:

将isPlaying变量置于全局。尽管您也可以完全删除它。您已经有了一个while循环条件,可以执行相同的操作。 将尝试值与零进行比较时,请删除等号。否则,当尝试次数达到零时,它将仍然运行。 当用户猜到正确答案时,请使用break语句,否则在猜到正确答案后仍将运行break语句

除此之外,您的代码还可以。以下是最终代码:

函数runGame(){
var num=Math.floor(Math.random()*10);
var猜测;
var=3;
警惕(“你有3次机会猜测1到10之间的心态!”);
而(尝试次数>0){
猜测=提示(“输入猜测:”;
如果(猜测>数值){
警惕(“太高了!”);
}
else if(猜测runGame()删除isplay并直接调用runGame(),而不是在while循环中,如果有机会,您可以中断执行,如果用户获胜,rest将尝试执行

function runGame() {

    var num = Math.floor(Math.random() * 10);
    var guess;
    var tries = 3;

    alert("You have 3 chances to guess a mindset between 1 and 10!");

    while (tries >= 0) {

        if (tries == 0) {
            alert("You have finished your chances");
            break;
        }

        guess = prompt("Enter a guess:");

        if (guess > num) {
            alert("Too high!");
        } else if (guess < num) {
            alert("Too low!");
        } else {
            alert("Exactly! " + num + " it is! You've won!");
            // reset tries back to three
            tries = 3;
        }

        tries--;

    }

}

runGame();
函数runGame(){
var num=Math.floor(Math.random()*10);
var猜测;
var=3;
警惕(“你有3次机会猜测1到10之间的心态!”);
while(尝试次数>=0){
如果(尝试==0){
警惕(“你已经完成了你的机会”);
打破
}
猜测=提示(“输入猜测:”;
如果(猜测>数值){
警惕(“太高了!”);
}else if(猜测
runGame()
删除while循环,即将
while(isplay=true){runGame();}
更改为
runGame()
,并将
while(trys>=0)
更改为
while(trys>0)
。删除
=