编写一个 Java 程序,使用 for 循环打印空心金字塔星形图案。此图案示例使用两个 if-else 语句和两个嵌套的 for 循环来迭代和显示一个空心金字塔。
package ShapePrograms;
import java.util.Scanner;
public class HollowPyramidPattern1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Please Enter Hollow Pyramid Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("---- Printing Hollow Pyramid Pattern of Stars ----");
int i, j, k;
for (i = 1 ; i <= rows; i++ )
{
for (j = 1 ; j <= rows - i; j++ )
{
System.out.print(" ");
}
if(i == 1 || i == rows) {
for (k = 1 ; k <= (i * 2) - 1; k++ )
{
System.out.print("*");
}
}
else {
for (k = 1; k <= (i * 2) - 1; k++ )
{
if(k == 1 || k == i * 2 - 1) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
Please Enter Hollow Pyramid Pattern Rows = 8
---- Printing Hollow Pyramid Pattern of Stars ----
*
* *
* *
* *
* *
* *
* *
***************
我们简化了上面的 代码,并删除了额外的 if-else 来显示空心金字塔星形图案。
package ShapePrograms;
import java.util.Scanner;
public class HollowPyramidPattern2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Please Enter Hollow Pyramid Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Hollow Pyramid Pattern of Stars");
int i, j, k;
for (i = 1 ; i <= rows; i++ )
{
for (j = 1 ; j <= rows - i; j++ )
{
System.out.print(" ");
}
for (k = 1; k <= (i * 2) - 1; k++ )
{
if(k == 1 || k == i * 2 - 1 || i == rows) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

在此空心金字塔星形图案程序中,我们将 for 循环替换为了 while 循环。
package ShapePrograms;
import java.util.Scanner;
public class HollowPyramidPattern3 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Please Enter Hollow Pyramid Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Hollow Pyramid Pattern of Stars");
int i = 1, j, k;
while( i <= rows)
{
j = 1 ;
while ( j <= rows - i )
{
System.out.print(" ");
j++;
}
k = 1;
while ( k <= (i * 2) - 1)
{
if(k == 1 || k == i * 2 - 1 || i == rows)
{
System.out.print("*");
}
else {
System.out.print(" ");
}
k++ ;
}
System.out.println();
i++;
}
}
}
Please Enter Hollow Pyramid Pattern Rows = 10
Printing Hollow Pyramid Pattern of Stars
*
* *
* *
* *
* *
* *
* *
* *
* *
*******************
Java 使用 do while 循环打印空心金字塔星形图案的程序
package ShapePrograms;
import java.util.Scanner;
public class HollowPyramidPattern4 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Please Enter Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Hollow Pyramid Pattern of Stars");
int i = 1, j, k;
do
{
j = 1 ;
do
{
System.out.print(" ");
} while (++j <= rows - i + 1);
k = 1;
do
{
if(k == 1 || k == i * 2 - 1 || i == rows)
{
System.out.print("*");
}
else {
System.out.print(" ");
}
} while ( ++k <= (i * 2) - 1);
System.out.println();
} while( ++i <= rows);
}
}
Please Enter Rows = 13
Printing Hollow Pyramid Pattern of Stars
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*************************
在此示例中,HollowMirroredRhombusPat 函数打印给定符号的空心金字塔图案。
package ShapePrograms;
import java.util.Scanner;
public class HollowPyramidPattern5 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Please Enter Rows = ");
int rows = sc.nextInt();
System.out.print("Enter Character for Hollow Pyramid Pattern = ");
char ch = sc.next().charAt(0);
System.out.println("---- Printing Hollow Pyramid Pattern ----");
HollowPyramidPatternPat(rows, ch);
}
public static void HollowPyramidPatternPat(int rows, char ch) {
int i, j, k;
for (i = 1 ; i <= rows; i++ )
{
for (j = 1 ; j <= rows - i; j++ )
{
System.out.print(" ");
}
for (k = 1; k <= (i * 2) - 1; k++ )
{
if(k == 1 || k == i * 2 - 1 || i == rows)
{
System.out.print(ch);
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Please Enter Rows = 15
Enter Character for Hollow Pyramid Pattern = #
---- Printing Hollow Pyramid Pattern ----
#
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
#############################