Java 程序:打印 8 星图案

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

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

输入:输入行数:4

输出:模式为:

  • *

  • *

  • *

  • *

  • *

  • *

  • *

程序 1:打印 8 星图案

在这个程序中,我们将看到如何使用 for 循环在 java 中打印一个 8 星图案。

算法:

  1. 开始
  2. 创建 Scanner 类的实例。
  3. 声明变量来存储行数。
  4. 要求用户初始化变量。
  5. 使用 for 循环打印图案。
  6. 在第一个内部循环中,从 j=1 迭代到 j=n,检查“if”条件,如果是真的,它将显示“space”,否则它会到达 else 部分并显示“*”符号。
  7. 如果第一个“如果”条件为假,那么将执行第二个内部 for 循环。在从 j=1 到 j=n 的第二次 for 循环迭代中,检查“if”条件如果为真,则显示“*”符号,否则显示“space”。
  8. 显示结果。
  9. 停止

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

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

输入行数:7


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

程序 2:打印 8 星图案

在这个程序中,我们将看到如何使用 while 循环在 java 中打印一个 8 星图案。

算法:

  1. 开始

  2. 创建 Scanner 类的实例。

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

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

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

  6. 在第一个内部 while 循环中,如果 while 的条件为真,则检查“if”条件,如果条件为真,则显示“space”,否则显示“*”,它将继续,直到内部 while 循环条件为假。

  7. 第二个内部 while 循环仅在外部 while 循环的 if 条件为 false 时执行,在第二个内部 while 循环中,首先检查 while 的条件,然后检查“if”条件,如果条件为 true,则显示“*”,否则显示“space”。

  8. 显示结果。

  9. 停止

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

//Java Program To Print 8 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=1;
        int j;
        int k=n*2-1;
        while(i<=k)
        {
            if(i==1 || i==n || i==k)
            {
                j=1;
                while(j<=n)
                {      
                    if(j==1  || j==n)
                    System.out.print(" ");
                    else
                    System.out.print("*");
                    j++;
               }
            }
            else
            {
                j=1;
                while(j<=n)
                {
                    if(j==1 || j==n)
                    System.out.print("*");
                    else
                    System.out.print(" ");
                    j++;
               }
           }
           System.out.println();
           i++;
        } 
    }
}

输入行数:8


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

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

Java 程序:程序 3:打印 8 星图案

在这个程序中,我们将看到如何使用 do-while 循环在 java 中打印一个 8 星图案。

算法:

  1. 开始

  2. 创建 Scanner 类的实例。

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

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

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

  6. 在第一个内部边做边循环中,如果“如果”条件为真,它将显示空格,否则它将显示“”,在第二个内部边做边循环中,如果“如果”条件为真,它将显示“”,否则它将显示空格。

  7. 外部 do-while 循环中的总代码将被执行,直到 while 条件为假,即 while(i<=k)。

  8. 显示结果。

  9. 停止

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

//Java Program To Print 8 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=1;
        int j;
        int k=n*2-1;
        do
        {
            if(i==1 || i==n || i==k)
            {
                j=1;
                do
                {      
                    if(j==1  || j==n)
                    System.out.print(" ");
                    else
                    System.out.print("*");
                    j++;
               }while(j<=n);
            }
        else
        {
               j=1;
               do
               {
                    if(j==1 || j==n)
                    System.out.print("*");
                    else
                    System.out.print(" ");
                    j++;
                }while(j<=n);
        }
        System.out.println();
        i++;
        }while(i<=k);            
    }
}

输入行数:6


  • *
  • *
  • *
  • *

  • *
  • *
  • *
  • *