Java 程序:打印右箭头星形图案

原文:https://www.studytonight.com/java-programs/java-program-to-print-the-right-arrow-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. 第一个外部 for 循环显示图案的一半,第二个外部 for 循环显示图案的下一半。

  9. 在第一个外部循环中,将检查条件是否为真,然后检查内部循环条件是否为真,然后检查“如果”条件。“如果”条件为真,则显示空格,否则显示我们给定要显示的字符。

  10. 内部 for 循环执行代码,直到条件为假。第一个外环将显示该模式。

  11. 显示模式。

  12. 停下来。

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

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

输入行数:5



  • *
  • *
  • *


Java 程序:程序 2:打印右箭头星形图案

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

算法:

  1. 开始

  2. 创建 Scanner 类的实例。

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

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

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

  6. While 循环首先检查条件是否为真,如果为真,则执行代码。

  7. 第一个外部 while 循环将显示图案的一半,第二个外部循环将显示图案的下一半。

  8. 第一个外部 while 循环的条件为真,然后进入内部循环,内部循环条件也为真,然后检查 if 条件,“if”条件为真,则显示空格,否则将显示字符“*”。内部循环将执行代码,直到条件为假。第一个外部循环执行代码,直到条件 i

  9. 光标到达下一行,然后将执行第二个外部 while 循环,直到条件为假。

  10. 显示结果。

  11. 停下来。

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

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

输入行数:5



  • *
  • *
  • *


Java 程序:程序 3:打印右箭头星形图案

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

算法:

  1. 开始

  2. 创建 Scanner 类的实例。

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

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

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

  6. 在第一个外部 do-while 循环中,它执行代码,然后检查条件 i

  7. 第二个外部 do-while 循环将执行代码,直到条件 i

  8. 显示结果。

  9. 停止

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

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

输入行数:8

              • T2】 T3】 T4】 T5】 T6】 T7】
  • *
  • *
          • T12】
          • T14】 T15】 * T15