编写一个 Java 程序,使用 for 循环打印沙漏字母图案。
package Alphabets;
import java.util.Scanner;
public class SandglassAlps1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Sandglass Pattern of Alphabets Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Sandglass Alphabets Pattern\n");
int i, j, k, alphabet = 64;
for (i = 1 ; i <= rows; i++ )
{
for (j = 1 ; j < i; j++ )
{
System.out.print(" ");
}
for(k = i; k <= rows ; k++)
{
System.out.print((char)(alphabet + k) + " ");
}
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++)
{
System.out.print((char)(alphabet + k) + " ");
}
System.out.println();
}
}
}

此 Java 程序使用 while 循环打印字母的沙漏图案。
package Alphabets;
import java.util.Scanner;
public class SandglassAlps2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Sandglass Pattern of Alphabets Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Sandglass Alphabets Pattern\n");
int i, j, k, alphabet = 64;
i = 1;
while (i <= rows )
{
j = 1 ;
while(j < i )
{
System.out.print(" ");
j++;
}
k = i;
while( k <= rows )
{
System.out.print((char)(alphabet + k) + " ");
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)
{
System.out.print((char)(alphabet + k) + " ");
k++;
}
System.out.println();
i--;
}
}
}
Enter Sandglass Pattern of Alphabets Rows = 13
Printing Sandglass Alphabets Pattern
A B C D E F G H I J K L M
B C D E F G H I J K L M
C D E F G H I J K L M
D E F G H I J K L M
E F G H I J K L M
F G H I J K L M
G H I J K L M
H I J K L M
I J K L M
J K L M
K L M
L M
M
L M
K L M
J K L M
I J K L M
H I J K L M
G H I J K L M
F G H I J K L M
E F G H I J K L M
D E F G H I J K L M
C D E F G H I J K L M
B C D E F G H I J K L M
A B C D E F G H I J K L M
此 Java 示例 使用 do while 循环显示字母的沙漏图案。
package Alphabets;
import java.util.Scanner;
public class SandglassAlps3 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Sandglass Pattern of Alphabets Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Sandglass Alphabets Pattern\n");
int i, j, k, alphabet = 64;
i = 1;
do
{
j = 1 ;
do
{
System.out.print(" ");
} while(j++ < i );
k = i;
do
{
System.out.print((char)(alphabet + k) + " ");
} 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
{
System.out.print((char)(alphabet + k) + " ");
} while(++k <= rows);
System.out.println();
} while( --i >= 1 ) ;
}
}
Enter Sandglass Pattern of Alphabets Rows = 15
Printing Sandglass Alphabets Pattern
A B C D E F G H I J K L M N O
B C D E F G H I J K L M N O
C D E F G H I J K L M N O
D E F G H I J K L M N O
E F G H I J K L M N O
F G H I J K L M N O
G H I J K L M N O
H I J K L M N O
I J K L M N O
J K L M N O
K L M N O
L M N O
M N O
N O
O
N O
M N O
L M N O
K L M N O
J K L M N O
I J K L M N O
H I J K L M N O
G H I J K L M N O
F G H I J K L M N O
E F G H I J K L M N O
D E F G H I J K L M N O
C D E F G H I J K L M N O
B C D E F G H I J K L M N O
A B C D E F G H I J K L M N O