【发布时间】: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