使用while循环时出现问题

时间:2015-09-29 00:05:07

标签: c++

给定正整数numInsects,写一个while循环,打印该数字加倍而不达到100.用空格跟随每个数字。循环后,打印换行符。例如:如果numInsects = 8,则打印:

8 16 32 64

#include <iostream>

using namespace std;

int main() {

   int numInsects = 0;

   numInsects = 8; // Must be >= 1

   while (numInsects < 100) {

   numInsects = numInsects * 2;



   cout << numInsects << " ";

   }

   cout << endl;

   return 0;

}

我的输出为:16 32 64 128 我完全知道如何获得输出我只是不知道如何将起始值设置为8。

1 个答案:

答案 0 :(得分:5)

在重新计算numInsects之前移动 cout 例如:

while (numInsects < 100) {
    cout << numInsects << " ";
    numInsects = numInsects * 2;
}
相关问题