Java 打印 A 到 Z 字母星形图案的程序

在本文中,我们将展示使用 for 循环、while 循环和函数打印 A 到 Z 字母星形图案的 Java 程序列表,并附带示例。

Java 打印字母 B 图案的程序

在这个 Java 图案程序中,我们展示了使用 for 循环、while 循环和函数打印字母 B 形状或图案的星星的步骤。

import java.util.Scanner;

public class Example
{
private static Scanner sc;

public static void main(String[] args)
{
sc = new Scanner(System.in);

System.out.print("Enter Numbers of Rows = ");
int rows = sc.nextInt();

for (int i = 0 ; i < rows; i++ )
{
System.out.printf("*");
for (int j = 0 ; j < 2 * rows - 1; j++ )
{
if ((i == 0 || i == rows - 1 || i == rows / 2) && (j < (2 * rows) - 2))
{
System.out.printf("*");
}
else if ((j == (2 * rows) - 2) && !(i == 0 || i == rows - 1 || i == rows / 2))
{
System.out.printf("*");
}
else
{
System.out.printf(" ");
}
}
System.out.println();
}
}
}
Enter Numbers of Rows = 15
***************************** 
*                            *
*                            *
*                            *
*                            *
*                            *
*                            *
***************************** 
*                            *
*                            *
*                            *
*                            *
*                            *
*                            *
***************************** 

在此字母 B 形状或图案程序中,我们将for 循环替换为while 循环来在每个位置打印星星。有关更多字母图案星形图案程序,请使用超链接。

import java.util.Scanner;

public class Example
{
private static Scanner sc;

public static void main(String[] args)
{
sc = new Scanner(System.in);
int rows, i, j;

System.out.print("Enter Numbers of Rows = ");
rows = sc.nextInt();

i = 0 ;
while (i < rows )
{
System.out.printf("*");
j = 0 ;
while ( j < rows - 1 )
{
if ((i == 0 || i == rows - 1 || i == rows / 2) && (j < rows - 2))
{
System.out.printf("*");
}
else if ((j == rows - 2) && !(i == 0 || i == rows - 1 || i == rows / 2))
{
System.out.printf("*");
}
else
{
System.out.printf(" ");
}
j++;
}
System.out.println();
i++;
}
}
}
Enter Numbers of Rows = 14
************* 
*            *
*            *
*            *
*            *
*            *
*            *
************* 
*            *
*            *
*            *
*            *
*            *
************* 

在此程序中,我们创建了一个 AlphabetBShape 函数来打印由行和列中的星形填充的字母 B 图案或形状。

import java.util.Scanner;

public class Example
{
private static Scanner sc;

public static void main(String[] args)
{
sc = new Scanner(System.in);

System.out.print("Enter Numbers of Rows = ");
int rows = sc.nextInt();

System.out.print("Enter Character = ");
char a = sc.next().charAt(0);

AlphabetBShape(rows, a);
}
public static void AlphabetBShape(int rows, char a)
{
for (int i = 0 ; i < rows; i++ )
{
System.out.printf("%c", a);
for (int j = 0 ; j < rows - 1; j++ )
{
if ((i == 0 || i == rows - 1 || i == rows / 2) && (j < rows - 2))
{
System.out.printf("%c ", a);
}
else if ((j == rows - 2) && !(i == 0 || i == rows - 1 || i == rows / 2))
{
System.out.printf("%c ", a);
}
else
{
System.out.printf(" ");
}
}
System.out.println();
}
}
}
Print Alphabet B Pattern