打印从A到Z的字母的Java程序

167 阅读1分钟

编写一个Java程序,使用for循环打印从A到Z的字母。在这个Java例子中,for循环将从字母A到Z进行迭代,并将它们作为输出打印。

public class AlphabetsAtoZ {
 
public static void main(String\[\] args) {
	
	char ch;
	
	for(ch = 'A'; ch <= 'Z'; ch++) {
		System.out.print(ch + " ");
	}
}

image.png

这个Java程序遍历了从65到90的ASCII码,代表字母A到Z,并将其打印出来。在这里,我们用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 

这个Java Alphabets from A to Z程序允许用户输入起始字母,并打印出其余的大写字母,直到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