C++使用 WHILE 循环的直角三角形代码

发布于 2024-12-01 15:38:24 字数 531 浏览 0 评论 0原文

我需要打印这个三角形:

*
**
***
****
*****
******
*******
********

使用 FOR 和 WHILE 循环。我需要帮助,我已经弄清楚了 for 循环版本,我只需将其转换为 while 循环,但我尝试的所有操作都没有给我正确的输出!任何帮助表示赞赏!

到目前为止我的代码:

#include <iostream>
using namespace std;
int main(int argc, char**argv) {

    int i = 1;
    int j = 1;
    int N = 8;

    while (i <= N)
    {
        i = i++;
        while(j <= i)
        {
            cout<<"*";
            j = j++;
        }
        cout<<endl;
    }
}

I need to print this triangle:

*
**
***
****
*****
******
*******
********

using a FOR and WHILE loop. I need help, I have already figured out the for loop version I just have to convert it to while loop but everything I try is not giving me the correct output! Any help is appreciated!

My code so far:

#include <iostream>
using namespace std;
int main(int argc, char**argv) {

    int i = 1;
    int j = 1;
    int N = 8;

    while (i <= N)
    {
        i = i++;
        while(j <= i)
        {
            cout<<"*";
            j = j++;
        }
        cout<<endl;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

猫腻 2024-12-08 15:38:24

我会给你一个提示(为了让你自己弄清楚):你忘记在内循环之后将 j 设置回 1

就像现在一样,当 j 变为 <= i 一次时,它会保持这种状态,并且永远不会再次进入内部循环。


另外,虽然它与您的问题没有直接关系,但请确保从不执行j = j++i = i++;只需执行 j++i++ (正如 Kshitij Mehta 在评论中所说)。如果您对原因感兴趣,可以阅读此问题及其答案

I'll give you a hint (in the interest of making you do some figuring out yourself): You're forgetting to set j back to 1 after the inner loop.

As it is now, when j gets to be <= i once, it stays that way and the inner loop is never entered again.


Also, while it's not directly related to your question, make sure never to do j = j++ or i = i++; just do j++ and i++ (as Kshitij Mehta said in the comments). If you're interested in why, you can read this question and its answers.

有深☉意 2024-12-08 15:38:24

规则是什么?

while (1)
{
    cout << "*" << '\n';
    cout << "**" << '\n';
    cout << "***" << '\n';
    cout << "****" << '\n';
    cout << "*****" << '\n';
    cout << "******" << '\n';
    cout << "*******" << '\n';
    cout << "********" << '\n';

    break;
}

What are the rules?

while (1)
{
    cout << "*" << '\n';
    cout << "**" << '\n';
    cout << "***" << '\n';
    cout << "****" << '\n';
    cout << "*****" << '\n';
    cout << "******" << '\n';
    cout << "*******" << '\n';
    cout << "********" << '\n';

    break;
}
岛徒 2024-12-08 15:38:24

我也会给你一个提示:i = i++; 并没有像你想象的那样做。

I'll give you a hint as well: i = i++; doesn't do what you think it does.

乖不如嘢 2024-12-08 15:38:24

我真的不知道如何使它比这更简洁:

#include <iostream>
#include <sstream>

int main()
{
    std::stringstream ss;
    int i = 10;
    while (i--)
        std::cout << (ss<<'*', ss).str() << std::endl;
}

或者对于循环,减少一行

for(int i=10; i--;)
        std::cout << (ss<<'*', ss).str() << std::endl;

如果您不介意一些效率较低的代码:

#include <iostream>
int main() { for(int i=1; i<10; std::cout << std::string(i++, '*') << std::endl); }

I don't really know how to make it more succinct than this:

#include <iostream>
#include <sstream>

int main()
{
    std::stringstream ss;
    int i = 10;
    while (i--)
        std::cout << (ss<<'*', ss).str() << std::endl;
}

or as for loop, cutting down a line

for(int i=10; i--;)
        std::cout << (ss<<'*', ss).str() << std::endl;

If you don't mind some less efficient code:

#include <iostream>
int main() { for(int i=1; i<10; std::cout << std::string(i++, '*') << std::endl); }
执着的年纪 2024-12-08 15:38:24

我看不到你的三角形,但我认为你需要在 j 上的 each 循环之前将 j 设置为 1:

while (i <= N)
{
    i++;
    j = 1;
    while(j <= i)
    {
        cout<<"*";
        j++;
    }
    cout<<endl;
}

I can't see your triangle, but I think you need to set j to 1 before each loop on j:

while (i <= N)
{
    i++;
    j = 1;
    while(j <= i)
    {
        cout<<"*";
        j++;
    }
    cout<<endl;
}
身边 2024-12-08 15:38:24
#include <iostream>   

using namespace std;    

int main() 
{
    int i, j, N=7;  
    while(i <= N)
    {  
        i++;  
        j = 1;  
        while(j <= i)
        {
            cout << "*";  
            j++;
        }
        cout << endl;  
    }  
}
#include <iostream>   

using namespace std;    

int main() 
{
    int i, j, N=7;  
    while(i <= N)
    {  
        i++;  
        j = 1;  
        while(j <= i)
        {
            cout << "*";  
            j++;
        }
        cout << endl;  
    }  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文