编写一个 Java 程序,使用 for 循环打印空心镜像菱形星形图案。此图案示例使用多个 if-else 语句和嵌套 for 循环来迭代和打印空心镜像菱形。
package ShapePrograms;
import java.util.Scanner;
public class HollowMirroredRhombus1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Hollow Mirrored Rhombus Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Hollow Mirrored Rhombus Star Pattern");
for (int i = 1 ; i <= rows; i++ )
{
for (int j = i ; j > 0; j-- )
{
System.out.print(" ");
}
if (i == 1 || i == rows)
{
for (int k = 1 ; k <= rows; k++ )
{
System.out.print("* ");
}
}
else
{
for (int k = 1 ; k <= rows; k++ )
{
if (k == 1 || k == rows) {
System.out.print("* ");
}
else {
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
Enter Hollow Mirrored Rhombus Pattern Rows = 7
Printing Hollow Mirrored Rhombus Star Pattern
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
虽然上述方法是正确的,但我们通过删除额外的 if-else 语句简化了空心镜像菱形代码。
package ShapePrograms;
import java.util.Scanner;
public class HollowMirroredRhombus2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Hollow Mirrored Rhombus Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Hollow Mirrored Rhombus Star Pattern");
for (int i = 1 ; i <= rows; i++ )
{
for (int j = 1 ; j < i; j++ )
{
System.out.print(" ");
}
for (int k = 1 ; k <= rows; k++ )
{
if (i == 1 || i == rows || k == 1 || k == rows) {
System.out.print("* ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

在此 Java 空心镜像菱形星形图案程序中,我们用 while 循环 替换了 for 循环。
package ShapePrograms;
import java.util.Scanner;
public class HollowMirroredRhombus3 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
int i = 1, j, k;
System.out.print("Enter Hollow Mirrored Rhombus Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Hollow Mirrored Rhombus Star Pattern");
while ( i <= rows)
{
j = 1 ;
while (j < i )
{
System.out.print(" ");
j++;
}
k = 1 ;
while( k <= rows )
{
if (i == 1 || i == rows || k == 1 || k == rows) {
System.out.print("* ");
}
else {
System.out.print(" ");
}
k++;
}
System.out.println();
i++ ;
}
}
}
Enter Hollow Mirrored Rhombus Pattern Rows = 10
Printing Hollow Mirrored Rhombus Star Pattern
* * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * *
Java 使用 do while 循环打印空心镜像菱形星形图案的程序
package ShapePrograms;
import java.util.Scanner;
public class HollowMirroredRhombus4 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
int i = 1, j, k;
System.out.print("Enter Hollow Mirrored Rhombus Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Hollow Mirrored Rhombus Star Pattern");
do
{
j = 1 ;
do
{
System.out.print(" ");
} while (j++ < i );
k = 1 ;
do
{
if (i == 1 || i == rows || k == 1 || k == rows) {
System.out.print("* ");
}
else {
System.out.print(" ");
}
} while( ++k <= rows ) ;
System.out.println();
} while ( ++i <= rows) ;
}
}
Enter Hollow Mirrored Rhombus Pattern Rows = 12
Printing Hollow Mirrored Rhombus Star Pattern
* * * * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * * * *
在此示例中,HollowMirroredRhombusPat 函数打印给定符号的空心镜像菱形图案。
package ShapePrograms;
import java.util.Scanner;
public class HollowMirroredRhombus5 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Hollow Mirrored Rhombus Pattern Rows = ");
int rows = sc.nextInt();
System.out.print("Enter Character for Mirrored Hollow Rhombus = ");
char ch = sc.next().charAt(0);
System.out.println("Printing Hollow Mirrored Rhombus Star Pattern");
HollowMirroredRhombusPat(rows, ch);
}
public static void HollowMirroredRhombusPat(int rows, char ch) {
for (int i = 1 ; i <= rows; i++ )
{
for (int j = 1 ; j < i; j++ )
{
System.out.print(" ");
}
for (int k = 1 ; k <= rows; k++ )
{
if (i == 1 || i == rows || k == 1 || k == rows) {
System.out.print(ch + " ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Enter Hollow Mirrored Rhombus Pattern Rows = 16
Enter Character for Mirrored Hollow Rhombus = $
Printing Hollow Mirrored Rhombus Star Pattern
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $