【问题标题】:Trouble with simple While and For loop简单的 While 和 For 循环的问题
【发布时间】:2014-04-02 13:56:14
【问题描述】:

我在下面的代码示例中遇到了一些问题,请多多包涵,我还是个初学者

var currentGen = 1;
var totalGen = 19;
var totalMW = 0;

totalMW = 62;
while (currentGen <= 4){
//Add 62 to the number of generators, beginning with Generator #1
console.log("Generator #" + currentGen + " is on, adding 62 MW, for a total of " +    totalMW + " MW!");
totalMW = totalMW + 62;
currentGen++;
}

for (var currentGen = 5; currentGen <= totalGen; currentGen++){
//add 124 to generators #5 - 19
console.log("Generator #" + currentGen + " is on, adding 124 MW, for a total of " +  totalMW + " MW!");
totalMW = totalMW + 124;
}


打印第 5 代时,它会打印

“5 号发电机开启,增加 124 兆瓦,总共 310 兆瓦!”

但我需要它从第 4 行开始添加 124,但它向第 5 代添加了 64 而不是 124。


我错过了什么?我应该在for 循环之前进行计算吗?

【问题讨论】:

    标签: javascript for-loop while-loop


    【解决方案1】:

    试试这个:

    var currentGen = 1;
    var totalGen = 19;
    var totalMW = 0;
    
    while (currentGen <= 4){
    //Add 62 to the number of generators, beginning with Generator #1
    totalMW = totalMW + 62;
    console.log("Generator #" + currentGen + " is on, adding 62 MW, for a total of " +    totalMW + " MW!");
    currentGen++;
    }
    
    for (var currentGen = 5; currentGen <= totalGen; currentGen++){
    //add 124 to generators #5 - 19
    totalMW = totalMW + 124;
    console.log("Generator #" + currentGen + " is on, adding 124 MW, for a total of " +  totalMW + " MW!");
    }
    

    这样你从零开始并在循环中执行所有加法。以前您在第一个循环之前将totalMW 设置为 62 ,然后在循环内再次增加总数 - 因此在前四次迭代之后您设置为 62(实际上是生成器 1 的一个增量)和然后再增加四次,总共增加五次而不是四次。

    【讨论】:

    • 我设置为 62,因为我需要它从 62 开始,就好像第一台发电机要通电并提供 62W 一样。我给出的示例的 var totalMW 从 0 开始,所以我以这种方式进行了更改。
    • 查看我的代码 - 我已将 totalMW = totalMW + 62; 移至 before 日志记录行。这意味着当记录第一行时,totalMW(循环前设置为零)将为 62,这正是您想要的。
    • 是的,我明白这一点,我发誓我试过了,但我想我只在其中一个循环中做到了。就是这样。谢谢!。并不是要浪费你的时间。
    • 没问题,很高兴能帮上忙。
    【解决方案2】:

    [编辑]:

    while (currentGen < 4){
    //Add 62 to the number of generators, beginning with Generator #1
    console.log("Generator #" + currentGen + " is on, adding 62 MW, for a total of " +    totalMW + " MW!");
    totalMW = totalMW + 62;
    currentGen++;
    }
    
    for (var currentGen = 4; currentGen <= totalGen; currentGen++){
    //add 124 to generators #5 - 19
    console.log("Generator #" + currentGen + " is on, adding 124 MW, for a total of " +  totalMW + " MW!");
    totalMW = totalMW + 124;
    }
    

    试试这个。

    【讨论】:

    • 这会删除生成器 4 的条目,试试这个。
    • 他说:“但我需要它从第 4 行开始添加 124,但它向第 5 代添加了 64 而不是 124”所以从逻辑上讲,我想这就是他想要的!
    • 初始值应该是62 :)
    【解决方案3】:

    这里是Working Fiddle

    使用

    while (currentGen <= 4){
    //Add 62 to the number of generators, beginning with Generator #1
    console.log("Generator #" + currentGen + " is on, adding 62 MW, for a total of " +    totalMW + " MW!");
    if(currentGen== 4) break;
    totalMW = totalMW + 62;
    currentGen++;
    }
    
    for (var currentGen = 5; currentGen <= totalGen; currentGen++){
    //add 124 to generators #5 - 19
     totalMW = totalMW + 124;
    console.log("Generator #" + currentGen + " is on, adding 124 MW, for a total of " +  totalMW + " MW!");
    
    }
    

    【讨论】:

    • 这不起作用 - 它打印 20 个生成器(0 到 19)而不是 19 个。
    猜你喜欢
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多