简体   繁体   English

我的while循环哪里出错了?

[英]Where am I going wrong with my while loop?

I am new to how loops work.我对循环的工作方式很陌生。 I want the loop to end if the input is N, for "Do you play minecraft?"如果输入为 N,我希望循环结束,因为“你玩我的世界吗?” If the user inputs Y, after the user inputs how many hours, I need it to continue to the calculation part and have it display.如果用户输入Y,在用户输入多少小时后,我需要它继续计算部分并让它显示。 My program does not do that.我的程序不这样做。 I'm not sure why it continues looping and doesn't go on to the next part.我不确定为什么它继续循环并且不继续下一部分。

 //Variables var hours; var total; var keepLooping = "Y"; var askingAQuestion = true; userInput = ""; //calculation and conversion total = hours * 7; hours = Number(hours); // Get the input from the user function myFunction() { while (keepLooping === "Y") { userInput = prompt("Do you play minecraft?" + " Y or N"); if (userInput === "N") { askingAQuestion = false; document.write("You should download the free trial!"); } else if (userInput === "Y") { document.write("That's great!"); hours = prompt("how many hours a day do you play minecraft?") } else if (hours >= 3) { document.write("if You play minecraft" + hours + "a day"); document.write("You play minecraft" + total + "hours a week!"); } } } myFunction();

Change the keepLooping value when meet conditions满足条件时更改keepLo​​oping值

//Variables
var hours;
var total;
var keepLooping = "Y";
var askingAQuestion = true;
userInput = "";

//calculation and conversion
total = hours * 7;
hours = Number(hours);

// Get the input from the user
function myFunction() {
    while (keepLooping === "Y") {
        userInput = prompt("Do you play minecraft?" + " Y or N");
        if (userInput === "N") {
            askingAQuestion = false;
            keepLooping =  "N";
            document.write("You should download the free trial!");
        } else if (userInput === "Y") {
            document.write("That's great!");
            hours = prompt("how many hours a day do you play minecraft?")
        } else if (hours >= 3) {
            document.write("if You play minecraft" + hours + "a day");
            document.write("You play minecraft" + total + "hours a week!");
        }

    }
}

myFunction();

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

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