Java 程序:打印空心菱形图案

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

在本教程中,我们将看到如何在 Java 中打印空心菱形图案。首先,我们将要求用户初始化行数。然后,我们将使用循环来打印两个空心的等边三角形,这两个三角形彼此背对,但具有相同的底边。但是在继续之前,如果你不熟悉 java 中循环的概念,那么一定要查看关于 Java 中循环的文章。

输入:输入行数:4

输出:模式为:

*

  • *

  • *

  • *

*

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

方法 1:使用 For 循环

方法 2:使用 While 循环

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

程序 1:打印空心钻石图案

在这个程序中,我们将看到如何使用 for 循环在 Java 中打印空心菱形图案。

算法:

  1. 开始
  2. 创建 Scanner 类的实例。
  3. 声明一个变量来存储行数。
  4. 要求用户初始化变量。
  5. 使用两个循环来打印图案。
  6. 在每个 for 循环中,再使用两个 for 循环和一个 if 语句来打印空格和模式。
  7. 使用第一个 for 循环打印上面的空心菱形图案。
  8. 使用第二个 for 循环打印下方的空心菱形图案。
  9. 显示结果。
  10. 停止

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

//Java Program to print the hollow diamond pattern
import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
       int i,j;
       Scanner scan=new Scanner(System.in);
       System.out.println("Enter the number of rows");
       int rows=scan.nextInt();//get input from user
       //print upper triangle
       for(i=1; i<=rows; i++)
       {
            for(j=rows; j>i; j--)
            {
                System.out.print(" ");//print space
            }
            System.out.print("*");  //print star
            for(j=1; j<(i-1)*2; j++)
            {
                System.out.print(" ");
            }
            if(i==1)
            {
                System.out.print("\n");//move to next line
            }
            else
            {
                System.out.print("*\n");
            }
        }
        //print lower triangle
        for(i=rows-1; i>=1; i--)
        {
            for(j=rows; j>i; j--)
            {
                System.out.print(" ");
            }
            System.out.print("*");
            for(j=1; j<(i-1)*2; j++)
            {
                System.out.print(" ");
            }
            if(i==1)
            {
                System.out.print("\n");
            }
            else
            {
                 System.out.print("*\n");
            }
        }
    }
}

输入行数:7 *

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

程序 2:打印空心钻石图案

在这个程序中,我们将看到如何使用 while 循环在 Java 中打印空心菱形图案。

算法:

  1. 开始
  2. 创建 Scanner 类的实例。
  3. 声明变量来存储行数和模式符号。
  4. 要求用户初始化这些变量。
  5. 使用两个 while 循环来打印图案。
  6. 在每个 while 循环中,使用另外两个 while 和一个 if 语句来打印空格和模式。
  7. 使用第一个 while 循环打印上面的空心菱形图案。
  8. 使用第二个 while 循环打印下方的空心菱形图案。
  9. 显示结果。
  10. 停止

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

//Java Program to print the hollow diamond pattern
import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
       int i,j;
       Scanner scan=new Scanner(System.in);
       System.out.println("Enter the number of rows");
       int rows=scan.nextInt();   //get input from user
       i=1; 
       //print upper triangle
       while(i<=rows)
       {
           j=rows;
           while(j>i)
           {
              System.out.print(" ");
              j--;
           }
           System.out.print("*");
           j=1; 
           while(j<(i-1)*2)
           {
              System.out.print(" ");
              j++;
           }
           if(i==1)
           {
              System.out.print("\n");
           }
           else
           {
              System.out.print("*\n");
           }
           i++;
        }
        //print lower triangle
        i=rows-1; 
        while(i>=1)
        {
            j=rows; 
            while(j>i)
            {
                System.out.print(" ");   //print space
                j--;
            }
            System.out.print("*");     //print star
            j=1; 
            while(j<(i-1)*2)
            {
                System.out.print(" ");
                j++;
            }
            if(i==1)
            {
                System.out.print("\n");   //move to next line
            }
            else
            {
                System.out.print("*\n");
            }
            i--;
        }
    }
}

输入行数:7 *

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