简体   繁体   English

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

[英]C++ code for right triangle using WHILE loop

I need to print this triangle: 我需要打印这个三角形:

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

using a FOR and WHILE loop. 使用FOR和WHILE循环。 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! 我需要帮助,我已经弄清楚了for循环版本,我只需要将其转换为while循环即可,但是我尝试的所有操作都没有给我正确的输出! 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;
    }
}

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. 我会给您一个提示(为了让您自己弄清楚自己的意思):您忘记在内部循环之后将j设置回1

As it is now, when j gets to be <= i once, it stays that way and the inner loop is never entered again. 就像现在一样,当j一次成为<= i时,它将保持这种状态,并且不再进入内部循环。


Also, while it's not directly related to your question, make sure never to do j = j++ or i = i++ ; 另外,尽管它与您的问题没有直接关系,但请确保不要执行j = j++i = i++ just do j++ and i++ (as Kshitij Mehta said in the comments). 只需执行j++i++ (如Kshitij Mehta在评论中所述)。 If you're interested in why, you can read this question and its answers . 如果您对为什么感兴趣,可以阅读此问题及其答案

I'll give you a hint as well: i = i++; 我也会给你一个提示: i = i++; doesn't do what you think it does. 不按照您的想法去做。

What are the rules? 规则是什么?

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

    break;
}

I can't see your triangle, but I think you need to set j to 1 before each loop on j: 我看不到您的三角形,但是我认为您需要在j的每个循环之前将j设置为1:

while (i <= N)
{
    i++;
    j = 1;
    while(j <= i)
    {
        cout<<"*";
        j++;
    }
    cout<<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); }
#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;  
    }  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM