简体   繁体   English

无法检测到为什么我的循环是无限的

[英]Can't detect why my loop is infinite

 var randomNum = Math.round(Math.random() * 100); guesses = prompt("guess a number between 1 and 100"); var scores = 0; while (randomNum < 100) { if (guesses < randomNum) { console.log(" too low.. continue") } else if (guesses > randomNum) { console.log("too high ... continue "); score++; } else if (guesses === randomNum) { console.log("great ... that is correct!!") } else { console.log("game over ... your guess was right " + scores + " times"); } } 
I have been struggling with the while loop concept for some time now and in order to confront my fears I decided to practice with some tiny exercises like the one above. 我一直在努力使用while循环概念,为了克服恐惧,我决定通过上述一些小练习进行练习。

您不会增加randomNum,因此它将始终处于无限循环中。

You initialize randonNum and guesses at the beginning of your code, but then you never change their values again. 您初始化randonNum并在代码的开头进行guesses ,但是随后您再也不会更改其值。 So, once you go inside the while loop and the condition starts out to be false, then there is nothing inside the while loop to ever change the outcome of the comparison condition. 因此,一旦进入while循环,并且条件开始为false,那么while循环内就没有任何东西可以改变比较条件的结果。 Thus, the condition is always false and you end up with an infinite loop. 因此,条件始终为假,您最终会陷入无限循环。 Your loop structure boils down to this: 您的循环结构可以归结为:

while (randomNum < 100) {
    // randomNum never changes
    // there is no code to ever break or return out of the loop
    // so loop is infinite and goes on forever
}

You can fix the problem by either putting a condition in the loop that will break out of the loop with a break or return or you can modify the value of randomNum in the loop such that eventually the loop will terminate on its own. 您可以通过解决这个问题把一个状态中循环,可中止循环与breakreturn ,也可以修改的值randomNum的循环,使得最终的循环将终止其自身。

In addition, guesses === randomNum will never be true because guesses is a string and randomNum is a number so you have to fix that comparison too. 此外, guesses === randomNum永远不会是正确的,因为guesses是一个字符串,而randomNum是一个数字,因此您也必须修复该比较。


It's not 100% clear what you want to achieve, but if you're trying to have the user repeatedly guess the number until they get it right, then you need to put a prompt() inside the while loop and a break out of the while loop when they get it right or ask to cancel: 并不是100%清楚您要实现什么,但是如果您想让用户反复猜测数字直到他们弄对了,那么您就需要在while循环中放入一个prompt()并打破当他们正确处理或要求取消时,执行while循环:

var randomNum = Math.round(Math.random() * 100);
var guess;
var score = 0;
while ((guess = prompt("guess a number between 1 and 100")) !== null) {
    // convert typed string into a number
    guess = +guess;
    if (guess < randomNum) {
        console.log(" too low.. continue")
    } else if (guess > randomNum) {
        console.log("too high ... continue ");
        score++;
    } else if (guess === randomNum) {
        console.log("great ... that is correct!!")
        console.log("score was: " + score);
        // when we match, stop the while loop
        break;
    }
}

the below line of code of your assign randomNum only one time hence it doesn't change 下面的代码只分配randomNum因此它不会改变

var randomNum = Math.round(Math.random() * 100);

so when you are trying to create the while loop the randomNum value remains same 因此,当您尝试创建while循环时, randomNum值保持不变

try changing the randomNum value in the while loop 尝试在while循环中更改randomNum

I think this is what you tried to achieve. 我认为这是您试图实现的目标。 Retry x number of times 重试x次

 var randomNum = Math.round(Math.random() * 100); var guesses; var scores = 0; var tries = 0 while (tries++ < 3) { // Loop if less than 3 tries, and increment guesses = prompt("guess a number between 1 and 100"); if (guesses < randomNum) { console.log(" too low.. continue") } else if (guesses > randomNum) { console.log("too high ... continue "); } else { // It's not to low, not to high. It must be correct score++; console.log("great ... that is correct!!"); randomNum = Math.round(Math.random() * 100); } } console.log("game over ... your guess was right " + scores + " times"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM