关闭。这个问题是 not reproducible or was caused by typos 。它目前不接受答案。
想改进这个问题?更新问题,使其成为 Stack Overflow 的 on-topic。
1年前关闭。
Improve this question
我正在尝试打印这样的二维数组。
1,2
3,4
5,6
7,8
直到 20
这是代码
#include <iostream>
using namespace std;
int main()
{
int A[10][2]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
for(int i=0;i<10;i++)
for(int j=0;j<2;j++)
{
cout<<A[i][j]<<" ";
}
cout << endl;
}
但是每次打印时,它都会以直线打印它们,例如 1 2 3 4 5 6............. 我会做错什么吗?
最佳答案
嘿,你忘了在第一个 for 循环之后添加 {}。
这是解决方案
#include <iostream>
using namespace std;
int main()
{
int A[10][2]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
for(int i=0;i<10;i++)
{
for(int j=0;j<2;j++)
{
cout<<A[i][j]<<" ";
}
cout << endl;
}
}
关于c++ - cout<<endl;不适用于打印二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62622456/