English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
在此程序中,您将学习如何在Java中使用for循环打印英文字母。您还将学习只打印大写和小写字母。
public class Characters { public static void main(String[] args) { char c; for(c = 'A'; c <= 'Z'; ++c) System.out.print(c + ""); } }
When running the program, the output is:
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
You can iterate from A to Z using a for loop because they are stored as ASCII characters in Java.
Therefore, inside, you can65to90 to print English letters.
Just a slight modification can display lowercase letters, as shown in the following example.
public class Characters { public static void main(String[] args) { char c; for(c = 'a'; c <= 'z'; ++c) System.out.print(c + ""); } }
When running the program, the output is:
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
You just need to replace 'A' with 'a' and 'Z' with 'z' to display lowercase letters. In this case, you will iterate through the97to122.