简体   繁体   English

C++ 中使用 while 循环的直角三角形星形图案

[英]Right angle triangle star pattern in C++ using while loops

I want a pattern in which for n=4 in 1st row it has 4 stars and in 2nd row I has 1 space & 3 stars and in 3rd row, it has 2 spaces and 2 stars and so on.我想要一个模式,其中第一行的 n=4 有 4 颗星,第二行有 1 个空格和 3 颗星,第三行有 2 个空格和 2 颗星,依此类推。

****
 ***
  **
   *

The code, I tried to solve this.代码,我试图解决这个问题。

#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    cout << endl;

    int i = 1;
    while (i <= n)
    {
        //Printing Spaces

        int space = i - 1;
        while (space)
        {
            cout << " ";
            space++;
        }

        //Printing Stars

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

    return 0;
}

In your while (space) loop you aren't comparing space to anything, so it assumes that the expression is always true.在您的while (space)循环中,您没有将space与任何东西进行比较,因此它假定表达式始终为真。

Here's a simplified way to do it:这是一种简化的方法:

#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    cout << endl;

    int i = 1;
    while (i <= n)
    {
        // print i-1 spaces
        for (int j = i-1; j >= 1; j--)
        {
            cout << " ";
        }
        // print n-i+1 stars
        for (int j = n; j >= i; j--){
            cout << "*";
        }
        cout << endl;
        i++;
    }

    return 0;
}
#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    cout << endl;

    int i = 1;
    while (i <= n)
    {

        int space = i - 1;  
        while (space>=1)  // change 1.1
        {
            cout << " ";
            space--;      // change 1.2
        }

        int j = i;        // change2
        while (j <= n)
        {
            cout << "*";
            j++;
        }
        cout << endl;
        i++;
    }

    return 0;
}

I made only 2 changes in your code so it can work.我只对您的代码进行了 2 处更改,因此它可以工作。 1st one第一个

while (space>=1)而(空间> = 1)

what you are doing is you are trying to add space in output so you add space variable in while() loop but that's not going to work because you have to decide first how many spaces you have to print according to that you have to put condition in while() loop.您正在做的是您尝试在 output 中添加空间,因此您在while()循环中添加space变量,但这不会起作用,因为您必须首先决定必须打印多少空格,根据您必须放置条件在while()循环中。 To achieve this space--;为了实现这个space--; added.添加。

For ex.例如。 line 4 i=4;第 4 行i=4; space want 3, so space=3; while(space>=1); space--;空间要 3,所以space=3; while(space>=1); space--; space=3; while(space>=1); space--; so while loop runs 3 time and print 3 gaps/spaces.所以 while 循环运行 3 次并打印 3 个间隙/空格。

2nd one第二个

int j = i;诠释 j = 我;

while (j <= n)而 (j <= n)

if you put j=1;如果你把j=1; then your gaps print properly but all stars print 4 times as loop runs 4 times always.然后你的间隙打印正确,但所有星星都打印 4 次,因为循环总是运行 4 次。 Due to this condition for i=1;由于i=1; But if you make j=i;但是如果你让j=i; loop runs 4 times for 1st line, 3 times for 2nd line,.....循环第一行运行 4 次,第二行运行 3 次,.....

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

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