各位大佬能帮我解释一下为什么行数打印不出来
#include <iostream>using namespace std;
int main()
{
int i, x, y = 0;
cout << "how many row would you like: ";
cin >> i;//行数
cout << "how many columns would you like: ";
cin >> x; //列数
while (y < i)
{
int z = 0;
while (z < x)
{
cout << "*";
x--;
}
cout << "\n";
y++;
}
} 第一个cin后的需要清空一下输入流,否则第二个cin给x赋了无效的值 大佬我还不是很懂能再仔细的说一下吗麻烦你了 本帖最后由 82457097 于 2019-3-13 12:13 编辑
while (y < i)
{
int z = 0;
while (z < x)
{
cout << "*";
x--;//将这里改为z++;你当初是给x赋值了 这里循环结束后 没有再次将x值还原 x就为零了
}
其实单步跑一遍错在哪就一目了然 加油! #include <iostream>
using namespace std;
int main()
{
int i, x, y = 0;
cout << "how many row would you like: ";
cin >> i;//行数
cout << "how many columns would you like: ";
cin >> x; //列数
int k = x; // -------------------->在此處加一個變數k=x;
while (y < i)
{
int z = 0;
while (z < x)
{
cout << "*";
x--;
}
x=k;// ----------------------------------->在此處重新給x=k
cout << "\n";
y++;
}
}
页:
[1]