需要 3 个独立的输出

Need 3 separate outputs

#include <iostream>
using namespace std;

int main (){

    int row, 
        column = 0,
        colCount = 3,
        rowCount = 3;

    //for loop
    for (column; column < colCount; column++){

        for(row = 0; row <= (rowCount - 1); row++){

            cout << column << " " << row;

            if(row < (rowCount - 1)){
                cout << ", ";           
            }

        }

        cout << endl;

    }

    //while loop
    while(column < colCount){

        while(row < rowCount){

            cout << column << " "<< row;

            if(row < (rowCount - 1)){
                cout << ", ";           
            }

            row++;          

        }

        cout << endl;   
        column += 1;
        row = 0;
    }

    //do while loop
    do{
        do{
            cout << column << " "<< row;

            if(row < (rowCount - 1)){
                cout << ", ";           
            }

            row++;  
        }while(row < rowCount);

        cout << endl;
        column +=1;
        row = 0;
    }while(column < colCount);

}

注释掉 2/3 循环时,每个循环都会产生所需的输出。 总而言之,它似乎 运行 彼此叠加并增加了额外的输出。

当前输出:

0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2
3 3

想要的输出:

0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2

0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2

0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2

如何从每个循环中获取输出?

1) 你应该在每个循环开始时将column初始化为0.
2) 在 whiledo 循环中,在进入内部循环

之前将 row 初始化为 0

最后的输出3 3是由于do-while循环中的一个入口。

您可以保留 for 循环:

    for (; column < colCount; column++){
        for(row = 0; row <= (rowCount - 1); row++){
            std::cout << column << " " << row;
            if(row < (rowCount - 1))
                std::cout << ", ";
        }
        std::cout << std::endl;
    }

现在 columnrow 在上面的 for-loop 中变为 3,这使得 while loop 永远不会被执行。因此,您需要将它们都设为 0.

而且,第三个 do-while 循环总是在任何条件检查之前执行,这就是为什么你得到 3 3

无论如何,这是解决您问题的方法。

#include <iostream>

int main ()
{
    int row, column = 0, colCount = 3, rowCount = 3;

    //for loop
    for (column; column < colCount; column++){
        for(row = 0; row <= (rowCount - 1); row++){
            std::cout << column << " " << row;
            if(row < (rowCount - 1))
                std::cout << ", ";
        }
        std::cout << std::endl;
    }
    std::cout<<std::endl;
    //while loop
    column = 0;
    row = 0;
    while(column < rowCount){
        while(row < rowCount){
            std::cout << column << " "<< row;
            if(row < (rowCount - 1))
                std::cout << ", ";
            row++;          
        }
        std::cout << std::endl;   
        column += 1;
        row = 0;
    }
    //do while loop
    std::cout<<std::endl;
    column = 0;
    row = 0;
    do{
        do{
            std::cout << column << " "<< row;
            if(row < (rowCount - 1))
                std::cout << ", ";
            row++;  
        }while(row < rowCount);
        std::cout << std::endl;
        column +=1;
        row = 0;
    }while(column < colCount);
    return 0;
}