使用 for 循环编写一个 Java 程序,打印从 A 到 Z 的字母。在此示例中,for 循环将从字母 A 迭代到 Z 并将它们打印为输出。
public class AlphabetsAtoZ {
public static void main(String[] args) {
char ch;
for(ch = 'A'; ch <= 'Z'; ch++) {
System.out.print(ch + " ");
}
}
}

此 程序 迭代代表字母 A 到 Z 的 ASCII 码从 65 到 90,并打印它们。在这里,我们使用了 for 循环、while 循环和 do while 循环来向您展示可能性。
public class AlphabetsAtoZ2 {
public static void main(String[] args) {
for(int i = 65; i <= 90; i++)
{
System.out.printf("%c ", i);
}
System.out.println();
int j = 65;
while(j <= 90)
{
System.out.printf("%c ", j);
j++;
}
System.out.println();
int k = 65;
do
{
System.out.printf("%c ", k);
} while(++k <= 90);
}
}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
此程序允许用户输入起始字母 A 并打印到 Z 的其余大写字母。
import java.util.Scanner;
public class AlphabetsAtoZ3 {
private static Scanner sc;
public static void main(String[] args) {
char ch, strChar;
sc= new Scanner(System.in);
System.out.print("Please Enter any Character = ");
strChar = sc.next().charAt(0);
for(ch = strChar; ch <= 'Z'; ch++) {
System.out.print(ch + " ");
}
}
}
Please Enter any Character = G
G H I J K L M N O P Q R S T U V W X Y Z