English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java 流程控制

Java 数组

Java 面向对象(I)

Java 面向对象(II)

Java 面向对象(III)

Java Exception Handling

Java 列表(List)

Java Queue(队列)

Java Map集合

Java Set集合

Java 输入输出(I/O)

Java Reader/Writer

Java 其他主题

Java程序使用循环从A到Z显示字符

Java example大全

在此程序中,您将学习如何在Java中使用for循环打印英文字母。您还将学习只打印大写和小写字母。

Example1:使用for循环显示大写的A到Z

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.

Example2: Display lowercase a to z using for loop

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.

Java example大全