简体   繁体   English

使用嵌套的while循环打印星形金字塔

[英]Using nested while loop to print pyramid of stars

I'm trying to print a pyramid of stars using nested while loops.我正在尝试使用嵌套的 while 循环打印星形金字塔。 I know I am able to achieve this using for loops but I want to do it with while loop instead.我知道我可以使用 for 循环来实现这一点,但我想用 while 循环来代替。 This is my code so far:到目前为止,这是我的代码:

public class WhileNest
{
    public static void main(String[]args)
    {
        int rows = 5, i = 1, j = 1;

        while(i <= rows)
        {
            while(j <= i)
            {
                System.out.print("*");
                j++;

            }
            System.out.print("\n");
            i++;

        }
    }
}

The output has to be like this: output 必须是这样的:

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

But my output is this:但我的 output 是这样的:

*
*
*
*
*

Any help is appreciated, thanks.任何帮助表示赞赏,谢谢。

you have to reset j like this:你必须像这样重置 j :

public class test {
    public static void main(String[] args) {
        int rows = 5, i = 1, j = 1;

        while (i <= rows) {
            while (j <= i) {
                System.out.print("*");
                j++;

            }
            System.out.print("\n");
            i++;
            j = 1;
        }
    }
}

You have forgotten to assign 1 to j at the end of the outer while loop.您忘记在外部 while 循环结束时将 1 分配给 j。

public class WhileNest {

    public static void main(String[] args) {
        int rows = 5, i = 1, j = 1;

        while (i <= rows) {
            while (j <= i) {
                System.out.print("*");
                j++;
            }
            System.out.print("\n");
            i++;
            j = 1;
        }
    }
}
public static void main(String[] args) {

    for(int i=0;i<10;i++){
        for(int k=0;k<i;k++){
            System.out.print("*");
        }
        System.out.println();
    }
}

Link: Printing Star using for-loop链接: 使用 for-loop 打印 Star

Link: Printing Star using while loop链接: 使用 while 循环打印 Star

Pyramid using two for loops: Pyramid 使用两个 for 循环:

String STAR = "*";
    String SPACE = " ";
    int SIZE = 10;
    for(int i=0;i<SIZE;i++) {
        int start = SIZE-i;
        int end = (SIZE*2) - SIZE + i;
        for(int j = 0; j<SIZE*2; j++) {
            if(j>=start && j<=end && j%2 == i%2) {
                System.out.print(STAR);
            } else {
                System.out.print(SPACE);
            }
        }
        System.out.println();
    }

output:输出:

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



Hope this the answer you are looking for...希望这是您正在寻找的答案...

*Instead of initializing "j" at the beginning, include it in the first while loop that'll do the work.( * will be printed at the beginning of each row) *不要在开头初始化“j”,而是将它包含在第一个可以完成工作的while循环中。(*将在每行的开头打印)

    public class WhileNest
    {
        public static void main(String[]args)
        {
            int rows = 5, i = 1;

            while(i <= rows)
            {
                int j = 1;
                while(j <= i)
                {
                    System.out.print("*");
                    j++;
                }
                System.out.print("\n");
                i++;

            }
        }
}
#include <stdio.h>

int main()
{
    int i=0,j=0,k=0;
    int r=5;
    while(i<=r)
    {
        while(j<=r-i)
        {
            printf(" ");
            j++;
        }
        while(k<=i)
        {
            printf (" *");
            k++;
        }
        printf("\n");
    i++;
    j=0;
    k=0;
    }
    
    return 0;
int rows = 5, i = 1, j = 1;

    while(i <= rows)
    {
        while(j <= i)
        {
            System.out.print("*");
            j++;
        }
        j=1;
        System.out.println();
        i++;
    }

You are not reassigning j to start from beginning.您没有重新分配j从头开始​​。 Your i and j were always staying same.您的ij始终保持不变。 Re-initialize j to 1 after the inner while loop as shown.如图所示,在内部 while 循环之后将j重新初始化为 1。 It will work它会工作

#include <stdio.h>
#include<math.h>
int main()
{
int i,j,n;
char c='*';
printf("Enter the size of the triangle:\n ");
scanf("%d",&n);
int width=n;
for(i=0;i<n;i++)
{
    for(j=0;j<i;j++)
    {
        if(j == 0)
        {

            printf("%*c",width,c);
             --width;
        }
        else
        {
            printf("%2c",c);
        }

    }
printf("\n");
}

}

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

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