C++ 用C+打印盒图案+;

C++ 用C+打印盒图案+;,c++,C++,我正在编写一个程序来打印两个方框,如下图所示: 我尝试了下面的程序,但它没有给出预期的输出 #include<iostream> using namespace std; int main() { // Considering each box as a separate one for(int i = 1; i <= 2; i++) { cout << "+---+ " << endl;

我正在编写一个程序来打印两个方框,如下图所示:

我尝试了下面的程序,但它没有给出预期的输出

#include<iostream>

using namespace std;

int main()
{
    // Considering each box as a separate one
    for(int i = 1; i <= 2; i++)
    {           
    cout << "+---+ " << endl;
    cout << "|  " << i << "| " << endl;
    cout << "|   | " << endl;
    cout << "|   | " << endl;
    cout << "+---+ ";
    }

    return 0;
}
请帮我找到我丢失的东西。
任何帮助或提示都将被高度赞赏。

不能将每个框视为单独的框,因为您将它们作为字符打印到一行一行的终端中。您必须先打印所有框的顶部,然后打印所有框的下一行,以此类推

如果要打印的框数可变,这将是解决方案:

int main() {
    using namespace std;
    unsigned numberOfBoxes = 10;

    for (unsigned i = 0; i < numberOfBoxes; ++i)
        cout << "+---+" << (i == numberOfBoxes - 1 ? "\n" : " ");
    for (unsigned i = 0; i < numberOfBoxes; ++i)
        cout << "|  " << i << "|" << (i == numberOfBoxes - 1 ? "\n" : " ");
    for (unsigned i = 0; i < numberOfBoxes; ++i)
        cout << "|   |" << (i == numberOfBoxes - 1 ? "\n" : " ");
    for (unsigned i = 0; i < numberOfBoxes; ++i)
        cout << "|   |" << (i == numberOfBoxes - 1 ? "\n" : " ");
    for (unsigned i = 0; i < numberOfBoxes; ++i)
        cout << "+---+ ";
}
intmain(){
使用名称空间std;
无符号numberofbox=10;
for(无符号i=0;icout如果您逻辑地跟踪for循环,您将看到如何一次打印一个框,堆叠在另一个框上

您需要确保在同一行上打印所有框的顶部,然后移动到所有框的主体,依此类推,直到所有框都“并行”打印

为此,您可以在其自己的for循环中打印每一行。例如:

#include<iostream>

using namespace std;

int main()
{
    for(int i = 1; i <= 2; i++) cout << "+---+ ";
    cout << endl;
    for(int i = 1; i <= 2; i++) cout << "|  " << i << "| ";
    cout << endl;
    for(int i = 1; i <= 2; i++) cout << "|   | ";
    cout << endl;
    for(int i = 1; i <= 2; i++) cout << "|   | ";
    cout << endl;
    for(int i = 1; i <= 2; i++) cout << "+---+ ";

    return 0;
}
#包括
使用名称空间std;
int main()
{

对于(inti=1;i对Maros答案的扩展:

如果以表格或矩阵形式打印时需要多行和多列:

int main() {
    using namespace std;
    unsigned numberOfColumns = 2, numberOfRows = 2;

    for (unsigned int i = 1; i <= numberOfRows; ++i) {
        for (unsigned j = 1; j <= numberOfColumns; ++j)
            cout << "+---+" << (j == numberOfColumns ? "\n" : " ");
        for (unsigned j = 1; j <= numberOfColumns; ++j)
            cout << "|  " << j + (numberOfColumns *(i-1)) << "|" << (j == numberOfColumns ? "\n" : " ");
        for (unsigned j = 1; j <= numberOfColumns; ++j)
            cout << "|   |" << (j == numberOfColumns ? "\n" : " ");
        for (unsigned j = 1; j <= numberOfColumns; ++j)
            cout << "|   |" << (j == numberOfColumns ? "\n" : " ");
        for (unsigned j = 1; j <= numberOfColumns; ++j)
            cout << "+---+ ";
        cout << endl;
    }   
    return 0;
}
intmain(){
使用名称空间std;
无符号numberOfColumns=2,numberOfRows=2;

对于(unsigned int i=1;i@downvoter),我尽了最大努力,但找不到解决方案。请告诉我downvoting的原因。我建议您发布您得到的内容。另外请注意,您的方框打印在同一行上(例如,第一行是
+--++--+
,都在一行中。如果你想使用普通的
std::cout
,你将找不到一个单独打印框的解决方案。试着打印两个框的第一行,继续打印两个框的第二行,以此类推…@wolff,我希望我可以按行打印,但我不能,因为每一行都有一行。)x必须单独处理。@BhawandeepSingla否决投票的原因可能是您没有显示当前尝试的结果,也没有进一步阐述您的预期。
int main() {
    using namespace std;
    unsigned numberOfColumns = 2, numberOfRows = 2;

    for (unsigned int i = 1; i <= numberOfRows; ++i) {
        for (unsigned j = 1; j <= numberOfColumns; ++j)
            cout << "+---+" << (j == numberOfColumns ? "\n" : " ");
        for (unsigned j = 1; j <= numberOfColumns; ++j)
            cout << "|  " << j + (numberOfColumns *(i-1)) << "|" << (j == numberOfColumns ? "\n" : " ");
        for (unsigned j = 1; j <= numberOfColumns; ++j)
            cout << "|   |" << (j == numberOfColumns ? "\n" : " ");
        for (unsigned j = 1; j <= numberOfColumns; ++j)
            cout << "|   |" << (j == numberOfColumns ? "\n" : " ");
        for (unsigned j = 1; j <= numberOfColumns; ++j)
            cout << "+---+ ";
        cout << endl;
    }   
    return 0;
}