【发布时间】:2022-01-10 17:24:25
【问题描述】:
形状应如下所示:shape
例如,这个图有 10 行。并且形状应该继续这种模式。
到目前为止,这是我的代码:
#include <iostream>
using namespace std;
int main()
{
int rows, a, b, c, d;
cout << "Enter the number of the rows: ";
cin >> rows;
for (a = 1; a <= rows; a++) {
for (b = 1; b <= a; b = a + 4) {
cout << " ******" << endl;
}
for (c = 2; c <= rows; c += 2) {
cout << " **********" << endl;
}
for (d = 3; d <= rows; d += 4) {
cout << "************" << endl;
}
}
return 0;
}
我无法将其恢复正常。比如我输入值 5 时,每行重复 5 次,但我希望行数为 5。
【问题讨论】:
-
附带说明,在一行中声明多个变量是一种不好的做法,应该避免。
-
这个循环似乎是错误的:
for (b=1; b<=a; b=a+4){b=a+4 b 只会小于或等于 a 1 次。
标签: c++ loops shapes nested-for-loop