Java 程序:打印空心镜像菱形星形图案
在本教程中,我们将看到如何在 Java 中打印中空镜像菱形星形图案。首先,我们将要求用户初始化行数。然后,我们将使用循环来打印中空的镜像菱形星形图案。但是在继续之前,如果你不熟悉 java 中循环的概念,那么一定要查看关于 Java 中循环的文章。
输入:输入行数:4
输出:模式为:
*
*
程序 1:打印空心镜像菱形
在这个程序中,我们将看到如何使用 for 循环打印中空镜像菱形星形图案。
算法:
- 开始
- 创建 Scanner 类的实例。
- 声明变量来存储行数和模式符号。
- 要求用户初始化这些变量。
- 使用 for 循环打印图案。
- 在这个 for 循环中,使用另外两个 for 循环和另一个 if-else 语句来打印模式。
- 使用第一个 for 循环打印空间,另一个 for 循环打印图案。
- 如果 if(i==1 || i==n)为真,则 for 循环的结构为(int j = 1;j < = n;j++)并打印从 j=1 到 n 的字符。
- 如果为假,循环的结构为(int j = 1;j < = n;j++)并在 j=1 或 j=n 时打印字符,在 if j!=1 和 j!=n。
- 显示输出。
- 停止
下面的例子说明了上述算法的实现。
//Java Program To Print Hollow Mirrored Rhombus
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=1;i<=n;i++)
{
for(int j=i;j>0;j--)
{
System.out.print(" ");
}
if(i==1 || i==n)
for(int j=1;j<=n;j++)
{
System.out.print(ch);
}
else
{
for(int j=1;j<=n;j++)
{
if(j==1 || j==n)
System.out.print(ch);
else
System.out.print(" ");
}
}
System.out.println();
}
}
}
输入行数:7 输入符号:*
- *
- *
- *
- *
- *
程序 2:打印空心镜像菱形
在这个程序中,我们将看到如何使用 while 循环打印中空镜像菱形星形图案。
算法:
开始
创建 Scanner 类的实例。
声明变量来存储行数和模式符号。
要求用户初始化这些变量。
使用 while 循环打印图案。
检查给定值 I 的条件,当(i<=n)时,如果该条件为真,则 j 初始化为 1。
在这个 while 循环中,使用另外两个 while 循环和另一个 if-else 语句来打印模式。
如果 if(i==1 || i==n)条件 a)为真,j=1,则内部 while 循环打印章程,直到 while 条件(j <=n)为假。b)false,当循环在 j=1 或 j=n 处打印 chart 时,转到 else 部分,直到 while(j<=n)条件为 false,在 j 处打印空格!=1 和 j!=n。
显示结果。
停下来。
下面的例子说明了上述算法的实现。
//Java Program To Print Hollow Mirrored Rhombus
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=1;
int j;
while(i<=n)
{
j=i;
while(--j>0)
{
System.out.print(" ");
}
if(i==1 || i==n)
{
j=1;
while(j <=n)
{
System.out.print(ch);
j++;
}
}
else
{
j=1;
while(j<=n)
{
if(j==1 || j==n)
System.out.print(ch);
else
System.out.print(" ");
j++;
}
}
System.out.println();
i++;
}
}
}
输入行数:7 输入符号:*
- *
- *
- *
- *
- *
程序 3:打印空心镜像菱形
在这个程序中,我们将看到如何使用 do-while 循环打印中空镜像菱形星形图案。
算法:
开始
创建 Scanner 类的实例。
声明变量来存储行数和模式符号。
要求用户初始化这些变量。
使用边做边循环打印图案。
对于给定的 I,j do 循环开始执行,内部 do 循环打印一个空格,然后检查 while 条件(–j > 0),如果为真,则打印空格,直到 while 条件(–j > 0)为假。
检查 if 条件。如果为真,则内部 do 循环打印一个章程,然后在(+j<=n)时检查条件,直到(+j<=n)为假时打印条件。
如果为假,则转到 else 部分,对于 j=1,内部循环打印一个字符,然后检查条件,而(+j<=n),则为假,因此打印空格,重复直到条件++j<=n 为假。
显示结果。
停止
下面的例子说明了上述算法的实现。
//Java Program To Print Hollow Mirrored Rhombus
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=1,j=1;
do
{
j=i;
do
{
System.out.print(" ");
}
while(--j>0);
if(i==1 || i==n)
{
j=1;
do
{
System.out.print(ch);
}
while(++j<=n);
}
else
{
j=1;
do
{
if(j==1 || j==n)
System.out.print(ch);
else
System.out.print(" ");
}while(++j<=n);
}
System.out.println();
++i;
} while(i<=n);
}
}
输入行数:8 输入符号:*
- *
- *
- *
- *
- *
- *