简体   繁体   English

我不明白为什么我会无限循环

[英]I can't figure out why I'm getting infinite loop

I'm in the process of learning this language and I was asked by my teacher to write some code.我正在学习这门语言,我的老师让我写一些代码。 Checked for all other posts about it and couldn't find the answer and I just don't understand why I'm getting infinite loop since the condition seems clear (to me at least haha): num1 has to be different from num2.检查了所有其他关于它的帖子,但找不到答案,我只是不明白为什么我会得到无限循环,因为条件似乎很清楚(至少对我来说哈哈):num1 必须与 num2 不同。 The code is supposed to keep making the question in num2 until the user types the same number as num1.该代码应该继续在 num2 中提出问题,直到用户键入与 num1 相同的数字。

var num1 = Number(prompt("Digite um número"))
console.log("Fight!")
arrayFinal = []
var num2 = Number(prompt("Que número o jogador 1 escolheu?"))

for (num1; num2 !== num1; num2) {
    arrayFinal.push(num2)
    console.log("O número chutado foi: ", num2)
    if (num2 > num1) {
        console.log("MISS! O número é menor!")
    } else if (num2 < num1) {
        console.log("MISS! O númenor é maior!")
    }
}
console.log("GAME OVER! O jogador 2 venceu! O número de tentativas foi: " +
    (arrayFinal.lenght + 1))

您想多次询问用户num2 ,因此它的prompt应该在循环中。

Your loop starts at num1 .您的循环从num1开始。 It also says that it will continue looping as long as num2 !== num1 .它还说只要num2 !== num1它将继续循环。 But at the end of line 6, you just simply put num2 , which does nothing to change num1.但是在第 6 行的末尾,您只需简单地放置num2 ,它不会改变 num1。 So, as long as num1 was previously unequal to num2 , it will always be unequal to num2 and thus make the loop infinite.所以,只要num1之前不等于num2 ,它就永远不等于num2 ,从而使循环无限。

You can declare num2 as null outside the loop, then do the prompting inside the loop.您可以在循环外将num2声明为 null,然后在循环内进行提示。 That way, when num2 equals num1 , num2 !== num1 returns false , and the loop will stop looping after that iteration.这样,当num2等于num1num2 !== num1返回false ,并且循环将在该迭代后停止循环。 Otherwise, it will continue looping and prompting again and again... until the user inputs the same number as num1 , then it will stop looping after the current iteration.否则,它将继续循环并一遍又一遍地提示……直到用户输入与num1相同的数字,然后在当前迭代后停止循环。

Also, Take lines 8-13 in the original code out of the loop, so that it does not get repeated as the code prompts repeatedly.另外,把原代码中的第8-13行去掉循环,这样就不会因为代码反复提示而重复了。

var num1 = Number(prompt("Digite um número"));
console.log("Fight!");
arrayFinal = [];

for (num1; num2 !== num1; num2) {
    var num2 = Number(prompt("Que número o jogador 1 escolheu?")); //dont forget 
    semicolon
    arrayFinal.push(num2);
    console.log("O número chutado foi: ", num2);
    if (num2 > num1) {
        console.log("MISS! O número é menor!");
    } else if (num2 < num1) {
        console.log("MISS! O númenor é maior!");
    }
}
console.log("GAME OVER! O jogador 2 venceu! O número de tentativas foi: " +
    (arrayFinal.length + 1));

暂无
暂无

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

相关问题 我不知道为什么我在这里得到“无法读取未定义的属性&#39;长度&#39;的错误”错误 - I can't figure out why I'm getting the “Cannot read property 'length' of undefined” error here 我不知道为什么会出现 ReferenceError: fileUploaded is not defined - I can't figure out why I'm getting a ReferenceError: fileUploaded is not defined 无法弄清楚为什么我得到“无法读取未定义的属性&#39;0&#39;”错误 - Can't figure out why I'm Getting “Cannot read property '0' of undefined” Error 我收到未处理的承诺拒绝错误,但不知道为什么 - I'm getting an Unhandled Promise Rejection error but can't figure out why 我做了一个无限循环,似乎无法弄清楚我做错了什么 - I've made an infinite loop and can't seem to figure out what I did wrong 我在JavaScript中变得未定义,无法弄清为什么 - I am getting undefined in JavaScript and can't figure it out why 无法弄清楚为什么我一直不确定 - Can't figure out why I keep getting undefined 这是一个无限循环,但是我无法为自己的生活弄清楚为什么 - This is an infinite loop, but I cannot for the life of me figure out why 我是 nodejs 的新手,我收到了参考错误:io is not defined ,我想不通 - I am new to nodejs and I'm getting reference error: io is not defined ,I can't figure it out 遵循教程但无法弄清楚为什么我会收到此消息:“类型'() =&gt; WordArray'.ts(2339) 上不存在属性'子字符串'” - Following a tutorial but can't figure out why I'm getting this message: "Property 'substring' does not exist on type '() => WordArray'.ts(2339)"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM