ในบทความนี้เราจะมาทำความเข้าใจวิธีการตรวจสอบว่าตัวอักษรเป็นตัวอักษรหรือไม่ ทำได้โดยการตรวจสอบว่าอักขระที่กำหนดอยู่ระหว่างตัวอักษร a ถึง z หรือ A ถึง Z
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
ป้อนข้อมูล
สมมติว่าข้อมูลที่เราป้อนคือ −
Enter a character: H
ผลผลิต
ผลลัพธ์ที่ต้องการจะเป็น −
The character H is an alphabet
อัลกอริทึม
Step 1 - START Step 2 - Declare a character value namely my_input Step 3 - Read the required values from the user/ define the values Step 4 - Using an if-else condition, check if the input value lies in between ‘a’ and ‘z’ or ‘A’ and ‘Z’ using comparison operators ‘>=’ and ‘<=’ . If true, its an alphabet, else its not an alphabet/ Step 5 - Display the result Step 6 - Stop
ตัวอย่างที่ 1
ที่นี่ ผู้ใช้ป้อนอินพุตตามข้อความแจ้ง คุณสามารถลองใช้ตัวอย่างนี้ในเครื่องมือกราวด์เขียนโค้ดของเราได้ .
import java.util.Scanner; public class Alphabet { public static void main(String[] args) { char my_input; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the character : "); my_input = my_scanner.next().charAt(0); if( (my_input >= 'a' && my_input <= 'z') || (my_input >= 'A' && my_input <= 'Z')) System.out.println("The character " +my_input + " is an alphabet"); else System.out.println("The character " +my_input + " is not an alphabet"); } }
ผลลัพธ์
Required packages have been imported A reader object has been defined Enter the character : H The character H is an alphabet
ตัวอย่างที่ 2
ในที่นี้ มีการกำหนดจำนวนเต็มก่อนหน้านี้ และเข้าถึงและแสดงค่าบนคอนโซล
public class Alphabet { public static void main(String[] args) { char my_input; my_input = 'H'; System.out.println("The Alphabet is defined as " +my_input); if( (my_input >= 'a' && my_input <= 'z') || (my_input >= 'A' && my_input <= 'Z')) System.out.println("The character " +my_input + " is an alphabet."); else System.out.println("The character " +my_input + " is not an alphabet."); } }
ผลลัพธ์
The Alphabet is defined as H The character H is an alphabet.