c++ - cout<<endl;不适用于打印二维数组

标签 c++ arrays endl

关闭。这个问题是 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/

相关文章:

c++ - 是std::memomove在同一对象之间始终是安全的

javascript - 单击时将数组中的项目插入到 div 中

java - 反转 int 数组,但结果只完成了一半

c++ - endl 不在输出文件中创建新行

c++ - 在 endl 上使用带有重载 << 运算符的 ofstream* 包装类

c++ - 添加 endl 修复代码

c++ - GMP 使用什么算法进行素数场反转 (mpz_invert)?

c++ - 检测多个枚举项何时映射到相同的值

c++ - 二叉树 - 复制构造函数

c++ - 子数组中两个数字的相同出现(连续)