ในบทความนี้ เราจะเข้าใจวิธีการพิมพ์ตัวอักษรจาก A ถึง Z หรือ a ถึง z ใน Java ทำได้โดยใช้ for-loop แบบง่าย
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
ป้อนข้อมูล
สมมติว่าข้อมูลที่เราป้อนคือ −
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