我如何结束javascript程序中期程序

时间:2015-01-26 06:01:39

标签: javascript

我需要一些帮助来结束我的计划。是否有命令在中途结束?在这个简短的猜测数字游戏我让一切顺利,除非第一人赢,因为它必须完成循环,即使在游戏结束后

 var rdmNumber = Math.random();
 var timesNumber = rdmNumber * 100;
 var theNumber = Math.round(timesNumber);
 var playerOne = prompt("Player 1 please enter your name...");
 var playerTwo = prompt("Player 2 please enter your name...");
 while (userInput != theNumber) {
     var userInput = prompt(playerOne + ", Take a Guess (0-100)");
     if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerOne + "      has won!");
     } else if (userInput < theNumber) {
         alert("Higher");
     } else {
         alert("Lower");
     }
     var userInput = prompt(playerTwo + ", Take a Guess (0-100)");
     if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerTwo + "  has won!");
     } else if (userInput < theNumber) {
         alert("Higher");
     } else {
         alert("Lower");
     }
 }

4 个答案:

答案 0 :(得分:1)

使用休息;像这样打破while循环:

 var rdmNumber = Math.random();
 var timesNumber = rdmNumber * 100;
 var theNumber = Math.round(timesNumber);
 var playerOne = prompt("Player 1 please enter your name...");
 var playerTwo = prompt("Player 2 please enter your name...");
 while (userInput != theNumber) {
     var userInput = prompt(playerOne + ", Take a Guess (0-100)");
     if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerOne + "      has won!");
         break; // it will break the while loop
     } else if (userInput < theNumber) {
         alert("Higher");
     } else {
         alert("Lower");
     }
     var userInput = prompt(playerTwo + ", Take a Guess (0-100)");
     if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerTwo + "  has won!");
         break; // it will break the while loop
     } else if (userInput < theNumber) {
         alert("Higher");
     } else {
         alert("Lower");
     }
 }

或者你可以将某个过程定义为一个函数,当有人赢了游戏时会返回一个值,它将结束:

function game() {
    var rdmNumber = Math.random();
    var timesNumber = rdmNumber * 100;
    var theNumber = Math.round(timesNumber);
    var playerOne = prompt("Player 1 please enter your name...");
    var playerTwo = prompt("Player 2 please enter your name...");
    while (userInput != theNumber) {
      var userInput = prompt(playerOne + ", Take a Guess (0-100)");
      if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerOne + "      has won!");
         return ; // it will end the function
      } else if (userInput < theNumber) {
         alert("Higher");
      } else {
         alert("Lower");
      }
      var userInput = prompt(playerTwo + ", Take a Guess (0-100)");
      if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerTwo + "  has won!");
         return ; // it will end the function
      } else if (userInput < theNumber) {
         alert("Higher");
      } else {
         alert("Lower");
      }
    }
}

game();

答案 1 :(得分:0)

我认为您正在寻找continue

while (userInput != theNumber) {
     var userInput = prompt(playerOne + ", Take a Guess (0-100)");
     if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerOne + "      has won!");
         continue;
     } else if (userInput < theNumber) {
         alert("Higher");
     } else {
         alert("Lower");
     }
     var userInput = prompt(playerTwo + ", Take a Guess (0-100)");
     if (userInput == theNumber) {
         alert("You Guessed it! " + userInput + " is correct. " + playerTwo + "  has won!");
     } else if (userInput < theNumber) {
         alert("Higher");
     } else {
         alert("Lower");
     }
 }

答案 2 :(得分:0)

  

break语句,简要介绍了交换机   声明,用于提前退出循环,突破封闭   花括号。

     

要处理所有这些情况, JavaScript会提供中断和继续操作   语句即可。这些陈述用于立即出现   循环或分别开始任何循环的下一次迭代。

示例

<script type="text/javascript">
<!--
var x = 1;
document.write("Entering the loop<br /> ");
while (x < 20)
{
  if (x == 5){ 
     break;  // breaks out of loop completely
  }
  x = x + 1;
  document.write( x + "<br />");
}
document.write("Exiting the loop!<br /> ");
//-->
</script>

信息由TutorialsPoint.com

提供

答案 3 :(得分:0)

通过改变你的逻辑:

将玩家名称存储到数组中,使用0,1,0,1 ...数字改变转弯,
只使用一个提示!!当前玩家由玩家[turnNumber]

定义

var rnd = Math.round( Math.random() * 5), // 0 - 5
    res = -1, // Result
    pl = [],  // ["Ethan", "Roko"]
    t = 0;    // turn: 0,1,0,1... (0 = 1st player)

for(var i=0; i<2; i++) pl[i] = prompt("Player"+ (i+1) +", please enter your name:");

while (res !== rnd) {
    res = parseInt( prompt( pl[t] + ", take a Guess (0-5)"), 10);  
    alert( res===rnd ? (res +" is correct! "+ pl[t] +" has won!") : (res<rnd? "Higher" : "Lower"));
    t = ++t % 2; // increment turn and reset to 0 if === 2 Results in: 1,0,1,0...
}