Java 程序:打印倒星形图案

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

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

输入:输入行数:4

输出:模式为:




*

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

方法 1:使用 For 循环

方法 2:使用 While 循环

方法 3:使用边做边循环

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

程序 1:打印倒星形图案

在这个程序中,我们将看到如何使用 for 循环在 java 中打印倒星形模式。

算法:

  1. 开始
  2. 创建 Scanner 类的实例。
  3. 声明一个变量来存储行数。
  4. 要求用户初始化变量。
  5. 使用 for 循环打印图案。
  6. 若要遍历行,请运行具有(int I = n;I > 0;I–)。
  7. 若要遍历行,请运行内部循环。
  8. 如果 i>0 && j
  9. 如果 i>0&&j
  10. 显示结果。
  11. 停止

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

//Java Program To Print Inverted Star Pattern
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();     
        for(int i=n;i>0 ;i--)
        {
            for(int j=0;j<n-i;j++)
            {
                System.out.print(" ");
            }
            for(int j=0;j<(i*2)-1;j++)
            {
                System.out.print("*");
            }
           System.out.println();
        }  
    }
}

输入行数:7

                          • T2】 T3】 T4】 T5】 T6

程序 2:打印倒星形图案

在这个程序中,我们将看到如何使用 while 循环在 java 中打印倒星形模式。

算法:

  1. 开始
  2. 创建 Scanner 类的实例。
  3. 声明一个变量来存储行数。
  4. 要求用户初始化变量。
  5. 使用 while 循环打印图案。
  6. Outer while 循环迭代,直到 i>0 为 false。
  7. 如果条件 j++
  8. 如果条件 j++
  9. 显示结果。
  10. 停止

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

//Java Program To Print the Inverted Star Pattern
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();     
        int  i=n,j;
        while(i>0)
        {
            j=0;
            while(j++<n-i)
            {
                System.out.print(" "); 
            }
            j=0;
            while(j++<(i*2)-1)
            {
                System.out.print("*"); 
            }
            System.out.println();
            i--;
       }  
    }
}

输入行数:7

                          • T2】 T3】 T4】 T5】 T6

程序 3:打印倒星形图案

在这个程序中,我们将看到如何使用 do-while 循环在 java 中打印倒星形模式。

算法:

  1. 开始
  2. 创建 Scanner 类的实例。
  3. 声明一个变量来存储行数。
  4. 要求用户初始化变量。
  5. 使用边做边循环打印图案。
  6. 外部 do-while 循环迭代,直到 while(–I > 0)为假。
  7. 第一个内部循环打印空格,直到条件 j++
  8. 第二个内部循环打印字符,直到条件 j++
  9. 显示结果。
  10. 停止

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

//Java Program To Print the Inverted Star Pattern
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //Take input from the user
        //Create  an instance of the Scanner Class 
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        int n=sc.nextInt();     
        int  i=n,j;
        do
        {
            j=0;
            do
            {
                System.out.print(" ");
            }while(j++<n-i);
            j=0;   
            do
            {
                System.out.print("*");
            }while(j++<i*2-2);
            System.out.println();
        }while(--i>0);       
    }
}

输入行数:7

                          • T2】 T3】 T4】 T5】 T6