ในบทความนี้ เราจะเข้าใจวิธีการตรวจสอบว่าตัวอักษรเป็นสระหรือพยัญชนะ ตัวอักษรที่มี 'a' 'e' 'i' 'o' 'u' จะเรียกว่าสระ และตัวอักษรอื่นๆ ทั้งหมดจะเรียกว่าพยัญชนะ
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
ป้อนข้อมูล
สมมติว่าข้อมูลที่เราป้อนคือ −
Enter the character : i
ผลผลิต
ผลลัพธ์ที่ต้องการจะเป็น −
The character : i is a vowel
อัลกอริทึม
Step 1 - START Step 2 - Declare a char value namely my_input. Step 3 - Read the required values from the user/ define the values Step 4 - Using an if condition, check if the input is equal to 'a' 'e' 'I' 'o' 'u' values. If yes, its an vowel. Else it's a consonant. Step 5 - Display the result Step 6 - Stop
ตัวอย่างที่ 1
ที่นี่ ผู้ใช้ป้อนอินพุตตามข้อความแจ้ง คุณสามารถลองใช้ตัวอย่างนี้ในเครื่องมือกราวด์เขียนโค้ดของเราได้
.
import java.util.Scanner;
public class VowelAndConsonant {
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 == 'e' || my_input == 'i' || my_input == 'o' || my_input == 'u' )
System.out.println("The character : " +my_input + " is a vowel");
else
System.out.println("The character : " +my_input + " is a consonant");
}
} ผลลัพธ์
Required packages have been imported A reader object has been defined Enter the character : i The character : i is a vowel
ตัวอย่างที่ 2
ในที่นี้ มีการกำหนดจำนวนเต็มก่อนหน้านี้ และเข้าถึงและแสดงค่าบนคอนโซล
public class VowelAndConsonant {
public static void main(String[] args) {
char my_input = 'i';
System.out.println("The character is defined as :" +my_input);
if(my_input == 'a' || my_input == 'e' || my_input == 'i' || my_input == 'o' || my_input == 'u' )
System.out.println("The character : " +my_input + " is a vowel");
else
System.out.println("The character : " +my_input + " is a consonant");
}
} ผลลัพธ์
The character is defined as :i The character : i is a vowel