Java 程式使用迴圈顯示字母(A 到 Z)


在本文中,我們將瞭解如何使用 Java 列印從 A 到 Z 或者從 a 到 z 的字母。這可以透過一個簡單的 for 迴圈 來完成。

以下對其進行演示 -

輸入

假設我們的輸入是 -

A to 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

演算法

Step1- Start
Step 2- Declare a character: my_temp
Step 3- Run a for loop from A to Z and print the values using the increment operator
Step 4- Display the result
Step 5- Stop

示例 1

這裡,整數已經預先定義,其值在控制檯中訪問和顯示。

public class Alphabets {
   public static void main(String[] args) {
      char my_temp;
      System.out.print("Displaying Alphabets from A to Z \n");
      for(my_temp= 'A'; my_temp <= 'Z'; ++ my_temp)
      System.out.print(my_temp+ " ");
   }
}

輸出

Displaying Alphabets from A to 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

示例 2

這裡,整數已經預先定義,其值在控制檯中訪問和顯示。

public class Alphabets {
   public static void main(String[] args) {
      char my_temp;
      System.out.print("Displaying Alphabets from a to z \n");
      for(my_temp = 'a'; my_temp <= 'z'; ++my_temp)
      System.out.print(my_temp + " ");
   }
}

輸出

Displaying Alphabets from a to 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

釋出於:2024 年 6 月 21 日

10,000+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告