编写一个 Java 程序,使用 for 循环打印空心沙漏星型图案。
package Shapes3;
import java.util.Scanner;
public class HollowSandglassStar1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
int i, j, k;
System.out.print("Enter Hollow Sandglass Star Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Hollow Sandglass Star Pattern");
for (i = 1; i <= rows; i++ )
{
for (j = 1; j < i; j++ )
{
System.out.print(" ");
}
for(k = i; k <= rows; k++)
{
if(i == 1 || k == i || k == rows)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
for (i = rows - 1; i >= 1; i-- )
{
for (j = 1; j < i; j++ )
{
System.out.print(" ");
}
for(k = i; k <= rows; k++)
{
if(i == 1 || k == i || k == rows)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}

此 Java 程序使用 while 循环显示星型的空心沙漏图案。
package Shapes3;
import java.util.Scanner;
public class HollowSandglassStar2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
int i, j, k;
System.out.print("Enter Hollow Sandglass Star Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Hollow Sandglass Star Pattern");
i = 1;
while( i <= rows )
{
j = 1;
while( j < i )
{
System.out.print(" ");
j++;
}
k = i;
while( k <= rows)
{
if(i == 1 || k == i || k == rows)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
k++;
}
System.out.println();
i++;
}
i = rows - 1;
while( i >= 1 )
{
j = 1;
while( j < i )
{
System.out.print(" ");
j++;
}
k = i;
while( k <= rows)
{
if(i == 1 || k == i || k == rows)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
k++;
}
System.out.println();
i--;
}
}
}
Enter Hollow Sandglass Star Pattern Rows = 7
Printing Hollow Sandglass Star Pattern
* * * * * * *
* *
* *
* *
* *
* *
*
* *
* *
* *
* *
* *
* * * * * * *
此 Java 程序使用 do while 循环打印星型的空心沙漏图案。
package Shapes3;
import java.util.Scanner;
public class HollowSandglassStar3 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
int i, j, k;
System.out.print("Enter Hollow Sandglass Star Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Hollow Sandglass Star Pattern");
i = 1;
do
{
j = 1;
do
{
System.out.print(" ");
} while( j++ <= i );
k = i;
do
{
if(i == 1 || k == i || k == rows)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
} while( ++k <= rows);
System.out.println();
} while(++i <= rows );
i = rows - 1;
do
{
j = 1;
do
{
System.out.print(" ");
} while( j++ <= i );
k = i;
do
{
if(i == 1 || k == i || k == rows)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
} while( ++k <= rows);
System.out.println();
} while(--i >= 1 );
}
}
Enter Hollow Sandglass Star Pattern Rows = 11
Printing Hollow Sandglass Star Pattern
* * * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * * *
在此 Java 示例中,HollowSandglassPattern 函数允许用户输入字符并打印给定字符的空心沙漏图案。
package Shapes3;
import java.util.Scanner;
public class HollowSandglassStar4 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Hollow Sandglass Star Pattern Rows = ");
int rows = sc.nextInt();
System.out.print("Enter Character for Hollow Sandglass Pattern = ");
char ch = sc.next().charAt(0);
System.out.println("Printing Hollow Sandglass Star Pattern");
HollowSandglassPattern(rows, ch);
}
public static void HollowSandglassPattern(int rows, char ch)
{
int i, j, k;
for (i = 1; i <= rows; i++ )
{
for (j = 1; j < i; j++ )
{
System.out.print(" ");
}
for(k = i; k <= rows; k++)
{
if(i == 1 || k == i || k == rows)
{
System.out.print(ch + " ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
for (i = rows - 1; i >= 1; i-- )
{
for (j = 1; j < i; j++ )
{
System.out.print(" ");
}
for(k = i; k <= rows; k++)
{
if(i == 1 || k == i || k == rows)
{
System.out.print(ch + " ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
Enter Hollow Sandglass Star Pattern Rows = 15
Enter Character for Hollow Sandglass Pattern = &
Printing Hollow Sandglass Star Pattern
& & & & & & & & & & & & & & &
& &
& &
& &
& &
& &
& &
& &
& &
& &
& &
& &
& &
& &
&
& &
& &
& &
& &
& &
& &
& &
& &
& &
& &
& &
& &
& &
& & & & & & & & & & & & & & &