【发布时间】:2016-10-04 08:48:39
【问题描述】:
我正在尝试将此 for 循环 转换为 do while 循环 并将其保留为 7 x 7 矩阵
For循环打印数字7 x 7:
for (int height = 0; height < 7; height++){
cout << numberMatrix[height][digitOne] << " ";
cout << numberMatrix[height][digitTwo] << " ";
cout << numberMatrix[height][digitThree] << " ";
cout << endl;
}
右输出:
这是我转换后的代码,但结果不正确。
For循环打印数字7 x 7:
int height = 0;
while (height < 7) {
cout << numberMatrix[height][digitOne] << " ";
cout << numberMatrix[height][digitTwo] << " ";
cout << numberMatrix[height][digitThree] << " ";
height++;
}
}
错误的输出:
【问题讨论】:
-
cout <<endl;你在while循环中忘记了这一行 -
请不要张贴图片,复制/粘贴文本输出
-
这两个代码sn-ps是等价的(除了while中缺少
cout<<endl) -
这是一个while循环。 do while 循环的结尾有条件,并且必须始终至少执行一次。因此,如果允许为空或 null 的情况,它不能轻易替换 for。
标签: c++ loops for-loop do-while