在 C++ 中将 for 循环转换为 do-while 循环
Converting for loop to do-while loop in C++
请帮我将此代码转换为 do-while
循环。
int i,j,k;
for (i=1; i<=5; i++) {
for (j=1; j<i; j++)
cout << " ";
for (k=1; k<=6-i; k++)
cout << "$";
cout << endl;
}
这就是我到目前为止得到的..
do{
j=5;
do{
do {
cout <<"$";
k--;
} while(k>=1);
j--;
} while (j>=i);
cout << endl;
i++;
} while(i<=5);
我似乎无法获得正确的输出...
预期输出应该是:
$$$$$
$$$$
$$$
$$
$
试试这个:
int i = 1;
do {
int j = 1, k = 1;
if (j < i) {
do {
cout << " ";
} while (++j < i);
}
do {
cout << "$";
} while (k++ < 6 - i);
cout << endl;
} while (i++ < 5);
请帮我将此代码转换为 do-while
循环。
int i,j,k;
for (i=1; i<=5; i++) {
for (j=1; j<i; j++)
cout << " ";
for (k=1; k<=6-i; k++)
cout << "$";
cout << endl;
}
这就是我到目前为止得到的..
do{
j=5;
do{
do {
cout <<"$";
k--;
} while(k>=1);
j--;
} while (j>=i);
cout << endl;
i++;
} while(i<=5);
我似乎无法获得正确的输出...
预期输出应该是:
$$$$$
$$$$
$$$
$$
$
试试这个:
int i = 1;
do {
int j = 1, k = 1;
if (j < i) {
do {
cout << " ";
} while (++j < i);
}
do {
cout << "$";
} while (k++ < 6 - i);
cout << endl;
} while (i++ < 5);