Java 程序:打印空心倒镜像直角三角形星形图案

原文:https://www.studytonight.com/java-programs/java-program-to-print-the-hollow-inverted-mirrored-right-triangle-star-pattern

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

输入:输入行数:6

输出:


  • *

  • *

  • *

**

*

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

方法 1:使用 For 循环

方法 2:使用 While 循环

方法 3:使用边做边循环

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

Java 程序:程序 1:打印空心倒镜直角三角形星形图案

在这个程序中,我们将看到如何使用 for 循环在 java 中打印中空的反向镜像直角三角形。

算法:

  1. 开始

  2. 创建 Scanner 类的实例。

  3. 声明变量来存储行数和模式符号。

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

  5. 使用三个循环来打印图案。

  6. 外部 for 循环将遍历行。

  7. 第一个内部 for 循环用于打印所需的空间。

  8. 使用 if-else 语句检查条件。

  9. 第二个内部 for 循环打印所需的模式符号。

  10. 显示结果。

  11. 停下来。

让我们看下面的例子来理解上面算法的实现。

//Java Program to print the Hollow Inverted Mirrored Right Triangle
import java.util.Scanner;
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(); 
        System.out.println("Enter the symbol: ");
        char ch = sc.next().charAt(0);
        for(int i=n;i>0;i--)
        {
            for(int j=n-i;j>0;j--)
            {
                System.out.print(" ");
            }
            if(i==1 || i==n)
            for(int j=1;j<=i;j++)
            {
                System.out.print(ch);
            }
            else
            {
                for(int j=1;j<=i;j++)
                {  
                    if(j==1 || j==i)
                    System.out.print(ch);
                    else
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

输入行数:8 输入符号:*


  • *
  • *
  • *
  • *
  • *

Java 程序:程序 2:打印空心倒镜直角三角形星形图案

在这个程序中,我们将看到如何使用 while 循环在 java 中打印中空的反向镜像直角三角形。

算法:

  1. 开始

  2. 创建 Scanner 类的实例。

  3. 声明变量来存储行数和模式符号。

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

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

  6. 外部 while 循环将遍历行。

  7. 第一个 while for 循环用于打印所需的空间。

  8. 使用 if-else 语句检查条件。

  9. 第二个内部 while 循环打印所需的模式符号。

  10. 显示结果。

  11. 停下来。

让我们看下面的例子来理解上面算法的实现。

//Java Program to print the Hollow Inverted Mirrored Right Triangle
import java.util.Scanner;
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(); 
        System.out.println("Enter the symbol: ");
        char ch = sc.next().charAt(0);
        int i=n;
        int j; 
        while(i>0)
        {
            j=n-i;
            while(j-->0)
            {
                System.out.print(" ");
            }
            if(i==1 || i==n)
            {
                j=1;
                while(j <=i)
                {
                    System.out.print(ch);
                    j++;
                }
            }
            else
            {
                j=1;
                while(j<=i)
                {
                    if(j==1 || j==i)
                    System.out.print(ch);
                    else
                    System.out.print(" ");
                    j++;
                }
            }
            System.out.println();
            i--; 
        } 
    }
}

输入行数:8 输入符号:*


  • *
  • *
  • *
  • *
  • *

Java 程序:程序 3:打印空心倒镜直角三角形星形图案

在这个程序中,我们将看到如何使用 do-while 循环在 java 中打印中空的反向镜像直角三角形。

算法:

  1. 开始

  2. 创建 Scanner 类的实例。

  3. 声明变量来存储行数和模式符号。

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

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

  6. 外部 do-while 循环将遍历行。

  7. 循环的第一个 do-while 用于打印所需的空间。

  8. 使用 if-else 语句检查条件。

  9. 第二个内部边做边循环打印所需的图案符号。

  10. 显示结果。

  11. 停下来。

让我们看下面的例子来理解上面算法的实现。

//Java Program to print the Hollow Inverted Mirrored Right Triangle
import java.util.Scanner;
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(); 
        System.out.println("Enter the symbol: ");
        char ch = sc.next().charAt(0);
        int i=n;
        int j; 
        do 
        {
            j=n-i;
            while(j-->0)
            {
                System.out.print(" ");
            }
            if(i==1 || i==n)
            {
                j=1;
                do
                {
                    System.out.print(ch);
                }while(++j <=i);
            }
            else
            {
                j=1;
                do
                {
                    if(j==1 || j==i)
                    System.out.print(ch);
                    else
                    System.out.print(" ");
                }while(++j<=i);
            }
            System.out.println();
        } while(--i>0); 
    }
}

输入行数:8 输入符号:*


  • *
  • *
  • *
  • *
  • *