显示从a到z的字母的Java程序

306 阅读1分钟

编写一个Java程序,使用for循环、while循环和do while循环显示从a到z的字母。在这个Java例子中,所有的循环都从字母a到z进行迭代,并将它们作为输出显示。

public class Alphabetsatoz1 { public static void main(String\[\] args) { char lwch;

	for(lwch = 'a'; lwch <= 'z'; lwch++) 
	{
		System.out.print(lwch + " ");
	}
	System.out.println();
	
	
	char lwch1 = 'a';
	while (lwch1 <= 'z') 
	{
		System.out.print(lwch1 + " ");
		lwch1++;
	}
	System.out.println();
	
	
	char lwch2 = 'a';
	do 
	{
		System.out.print(lwch2 + " ");
	} while (++lwch2 <= 'z');
}

image.png

这个显示a到z的Java程序迭代了从97到122的ASCII码,代表字母a到z并打印它们。

public class Example2 {

public static void main(String\[\] args) {
		
	for(int i = 97; i <= 122; i++) 
	{
		System.out.printf("%c ", i);
	}
}
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显示字母从a到z的程序允许用户输入开始的小写字母,并打印出剩余的字母,直到z。

import java.util.Scanner;

public class Example3 {

private static Scanner sc;

public static void main(String\[\] args) {
		
	char ch, strlwChar;
	sc= new Scanner(System.in);

	System.out.print("Please Enter any Char =  ");
	strlwChar = sc.next().charAt(0);
	
	for(ch = strlwChar; ch <= 'z'; ch++) {
		System.out.print(ch + " ");
	}
}
Please Enter any Char =  h
h i j k l m n o p q r s t u v w x y z