C++ 嵌套For循环在while循环中执行

C++ 嵌套For循环在while循环中执行,c++,C++,嗨,我读了关于家庭作业问题的指南,上面说要清楚地说明这是家庭作业。这是家庭作业,我花了45分钟反复尝试。我撞到墙了,需要帮助 我的任务是获取来自double For循环的代码,并将其转换为嵌套在For循环中的while循环。我已经成功地完成了。然而,第三部分是获取该代码并将外部for循环变成do-while循环。 如果输入为“4”,则输出需要每行递增一个“#” 下面是我编写的代码,我需要将外部for循环变成do-while循环: int main() { int side; c

嗨,我读了关于家庭作业问题的指南,上面说要清楚地说明这是家庭作业。这是家庭作业,我花了45分钟反复尝试。我撞到墙了,需要帮助

我的任务是获取来自double For循环的代码,并将其转换为嵌套在For循环中的while循环。我已经成功地完成了。然而,第三部分是获取该代码并将外部for循环变成do-while循环。 如果输入为“4”,则输出需要每行递增一个“#”

下面是我编写的代码,我需要将外部for循环变成do-while循环:

int main()
{
    int side;

    cout << "Enter a number: ";
    cin >> side;

    for (int i = 0; i < side; i++)
    {
        int j = i;
        while(j >= 0)
        {
            cout << "#";
            j--;
        }
        cout << "\n";
    }
}
intmain()
{
内侧;
侧边;
对于(int i=0;i=0)
{
cout=0)
{
cout
  • while(j>=side);
    应该是
    while(我建议

    int main()
    {
        int side;
        int i = 0;
        cout << "Enter a number: ";
        cin >> side;
    
        do
        {
            int j = i;
            while(j >= 0)
            {
               cout << "#";
               j--;
            }
            cout << "\n";
            i++;
        }while(i < side)
    }
    
    intmain()
    {
    内侧;
    int i=0;
    侧边;
    做
    {
    int j=i;
    而(j>=0)
    {
    
    你犯的第一个错误是:

    int i; //not initialized!
    /*...*/
    i++;
    
    而你甚至没有在你的do while状态下使用它

    所以
    while(j>=side);
    while(i>=side);

    事实上,这也不是真的。因为side是输入,所以您希望
    i
    检查它是否小于而不是大于输入。因此它是
    while(i

    另一件事是
    int j=side;
    ,当您减小
    j
    时,它将永远不会重置,因此您必须将其设置到do while循环中,并用
    i
    而不是side>初始化它

    无论如何,以下是完整的代码:

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int side;
        int i = 0;
    
        cout << "Enter a number: ";
        cin >> side;
        do
        {
            int j = i;
            while (j >= 0)
            {
                cout << "#";
                j--;
            }
            cout << "\n";
            i++;
        } while (i < side);
    
        return 0;
    }
    

    上面的答案解决了您的问题,但让我向您展示一种较短的方法:

    int main()
    {
        int side;
    
        std::cout << "Enter a number: ";
        std::cin >> side;
    
        int row = 1;
        do
        {
            int col = 0;
            while (col++ != row)
            {
                std::cout << "#";
            }
            std::cout << "\n";
        } while (row++ != side); //This way, the condition is checked (row != side), and then row is incremented
    }
    
    intmain()
    {
    内侧;
    std::cout>侧边;
    int行=1;
    做
    {
    int col=0;
    while(列++!=行)
    {
    
    std::cout你的循环中的
    i++;
    的目的是什么?我想你是我看到的第一个在发帖前尝试解决作业的人……哇。嘿,为什么do-while循环在它的状态下不使用i呢?不要破坏你的帖子。非常感谢你!我想我只是在凌晨3点的时候有严重的大脑迷雾:)但是在跟踪代码之后,它实际上是有意义的!现在我只需要错误处理,并让它在早上接收负片:)如果我遇到问题,让它与负片相同,我可以给你发送tmrw吗?为什么不可以。祝你好运!还有一件事,我需要使用“返回0”当编程时。我的代码运行得很好,没有它。而且@treycos也不使用它。因为
    int main()
    是一个返回
    int
    的函数,最好实际返回0(没有错误).如果你省略了,编译器会为你添加它。但是在我的演讲中,没有人会因为多写一点内容而感到痛苦。哦,好吧,我很困惑,因为我的老师有一些问题,而有些他省略了…我不知道编译器只是自动添加它…再次感谢Danny的帮助
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int side;
        int i = 0;
    
        cout << "Enter a number: ";
        cin >> side;
        do
        {
            int j = i;
            while (j >= 0)
            {
                cout << "#";
                j--;
            }
            cout << "\n";
            i++;
        } while (i < side);
    
        return 0;
    }
    
    Enter a number: 10
    #
    ##
    ###
    ####
    #####
    ######
    #######
    ########
    #########
    ##########
    
    int main()
    {
        int side;
    
        std::cout << "Enter a number: ";
        std::cin >> side;
    
        int row = 1;
        do
        {
            int col = 0;
            while (col++ != row)
            {
                std::cout << "#";
            }
            std::cout << "\n";
        } while (row++ != side); //This way, the condition is checked (row != side), and then row is incremented
    }