C++ 使用嵌套循环进行简单(对我来说很难)的输出

C++ 使用嵌套循环进行简单(对我来说很难)的输出,c++,loops,nested,C++,Loops,Nested,**我需要输出 1 2 3 4 5 6 7 8 9 使用嵌套循环** 我试过这个 for(int i=1;i<=6;i++) { cout<<endl; for(int j=1;j<=i and j<=3;++j) cout<<i++; } for(inti=1;ifor(inti=0;i

**我需要输出

1 2 3

4 5 6

7 8 9

使用嵌套循环**

我试过这个

for(int i=1;i<=6;i++)
 {
   cout<<endl;
   for(int j=1;j<=i and j<=3;++j)
   cout<<i++;
  }
for(inti=1;i
for(inti=0;i<3;i++){

对于(int j=1;j我发现这样的解决方案更干净:

for( int i = 1; i < 10; ++i )
{
    std::cout << i;
    if( i % 3 == 0 )
        std::cout << '\n';
}
for(int i=1;i<10;++i)
{

标准::cout
用于(int j=1;jt不是c,但C++。您看到的是Eric Lippert。我注意到的是,您在< <代码> < < /Cult>循环语句中,在for循环中,正在递增<代码> i <代码>。您只需要其中之一。@ Lundin:如果编译器不编译,则他的C++编译器严重损坏。我没有使用ISO64之类的东西。.h我刚刚使用了iostreamYou need
i<3
。就个人而言,我认为OP在体内增加i的方法更简洁。是的,我编辑了我的答案。你可以解释一下答案。我从0增加到3,所以你可以认为i是行数i*3是该行的第一个数字。然后,对于每个i*3(每行的第一个数字)将1,2,3增加为i*3+1,i*3+2,i*i需要一个嵌套循环
for( int i = 1; i < 10; ++i )
{
    std::cout << i;
    if( i % 3 == 0 )
        std::cout << '\n';
}