Java 程序:打印空心金字塔星形图案

原文:https://www.studytonight.com/java-programs/java-program-to-print-the-hollow-pyramid-star-pattern

在本教程中,我们将看到如何在 Java 中打印空心金字塔星形图案。首先,我们将要求用户初始化行数。然后,我们将使用循环来打印空心金字塔星形图案。但是在继续之前,如果你不熟悉 java 中循环的概念,那么一定要查看关于 Java 中循环的文章。

输入:输入行数:4

输出:模式为:

*





这可以通过使用以下方法来实现:

方法 1:使用 For 循环

方法 2:使用 While 循环

方法 3:使用边做边循环

让我们看看这些方法中的每一种,以便更好地理解。

Java 程序:程序 1:打印空心金字塔星形图案

在这个程序中,我们将看到如何使用 for 循环在 java 中打印空心金字塔星形图案。

算法:

  1. 开始

  2. 创建 Scanner 类的实例。

  3. 声明一个变量来存储行数。

  4. 要求用户初始化变量。

  5. 使用 for 循环打印图案。

  6. 使用外部 for 循环迭代从 1 到 n 的行。

  7. 使用第一个内部 for 循环从 1 迭代到 n,然后打印所需的空间。

  8. 在中使用另一个 for 循环来打印所需的空间。

  9. 使用 if-else 语句打印所需的模式。

  10. 如果“If”条件为真,则使用从 1 迭代到 i*(2-1)的 for 循环,并打印星形符号。

  11. 如果“If”条件为假,则下一个 for 循环打印 if(j==1 || j==i*2-1)条件为真的字符。否则,它会打印空间。

  12. 显示模式。

  13. 停下来。

下面的例子说明了上述算法的实现。

//Java Program to Print the Hollow Pyramid Star Pattern
import java.util.*;
public class Main
{
     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        int n=sc.nextInt(); 
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n-i;j++)
            {
                System.out.print(" ");
            }
            if(i==1 || i==n)
            for(int j=1;j<=i*2-1;j++)
            {
                System.out.print("*");
            }
            else
            {
                for(int j=1;j<=i*2-1;j++)
                {  
                    if(j==1 || j==i*2-1)
                    System.out.print("*");
                    else
                    System.out.print(" ");
                }
            }
            System.out.println();
        } 
    }
}

输入行数:8 *

  • *
  • *
  • *
  • *
  • *
  • *

Java 程序:程序 2:打印空心金字塔星形图案

在这个程序中,我们将看到如何使用 while 循环在 java 中打印空心金字塔星形图案。

算法:

  1. 开始

  2. 创建 Scanner 类的实例。

  3. 声明一个变量来存储行数。

  4. 要求用户初始化变量。

  5. 使用 while 循环打印图案。

  6. 使用外部 while 循环进行迭代,直到 i<=n。

  7. 使用内部 while 循环遍历列。

  8. 在外层,使用另一个 if 条件打印所需的空间。

  9. 如果 if(i==1 || i==n)条件为真,则第一个内部循环将打印字符,直到 j++<=i*2-1 条件为真。

  10. 如果条件为假,则如果(j==1 || j==(i2)-1)为真,则第二个内部循环打印字符,直到 while(j<=(i2)-1)为假,否则打印空格。

  11. 显示模式。

  12. 停下来。

下面的例子说明了上述算法的实现。

//Java Program to Print the Hollow Pyramid Star Pattern
import java.util.*;
public class Main
{
     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        int n=sc.nextInt(); 
        int i=1;
        int j;
        while(i<=n)
        {
            j=1;
            while(j++<=n-i)
            {
                System.out.print(" ");
            }
            if(i==1 || i==n)
            {
                j=1;
                while(j++<=i*2-1)
                {
                   System.out.print("*");
                }
            }
            else
            {
                j=1;
                while(j<=(i*2)-1)
                {
                    if(j==1 || j==(i*2)-1)
                    System.out.print("*");
                    else
                    System.out.print(" ");
                    j++;
                }
            }
            System.out.println();
            i++;
        } 
    }
}

输入行数:7 *

  • *
  • *
  • *
  • *
  • *

Java 程序:程序 3:打印空心金字塔星形图案

在这个程序中,我们将看到如何使用 do-while 循环在 java 中打印空心金字塔星形图案。

算法:

  1. 开始

  2. 创建 Scanner 类的实例。

  3. 声明一个变量来存储行数。

  4. 要求用户初始化变量。

  5. 使用边做边循环打印图案。

  6. 第一个内部 do-while 循环将迭代,直到条件++j <=n-i+1

  7. 在外部 do-while 内部,使用另一个 if 条件打印所需的空间。

  8. 如果“如果”条件为真,则内部边做边循环打印章程,直到条件(++j <=i*2-1)为假。

  9. 如果“If”条件为假,则下一个 do-while 循环打印 chart if if(j = = 1 | | j = = I 2-1)为真,直到 while(+j < = I 2-1)为假,否则打印空格。

  10. 显示模式。

  11. 停下来。

下面的例子说明了上述算法的实现。

//Java Program to Print the Hollow Pyramid Star Pattern
import java.util.*;
public class Main
{
     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        int n=sc.nextInt(); 
        int i=1;
        int j;
        do 
        {
            j=1;
            do
            {
                System.out.print(" "); 
            }while(++j <=n-i+1); 
            if(i==1 || i==n)
            {
                j=1;
                do
                {
                    System.out.print("*"); 
                }while(++j <=i*2-1); 
            }
            else
            {
                j=1;
                do
                {
                    if(j==1 || j==i*2-1)
                    System.out.print("*");
                    else
                    System.out.print(" ");
                }while(++j<=i*2-1);
            }
            System.out.println();
            ++i;
        } while(i<=n); 
    }
}

输入行数:7 *

  • *
  • *
  • *
  • *
  • *