Dalam artikel ini, kita akan melihat pemacu menu dilaksanakan menggunakan bahasa pengaturcaraan Java untuk menyemak sama ada aksara yang dimasukkan ialah nombor, rentetan atau aksara khas. Kami akan menggunakan suis case untuk melaksanakan aplikasi ini.
Suppose the entered character is ‘a’ then the output should be “Entered character is a String”.
Suppose the entered character is ‘1’ then the output should be “Entered character is a number”.
Suppose the entered character is ‘$’ then the output should be “Entered character is a Special character”.
Di Java, kami menggunakan fungsi isLetter, isDigit atau isWhitespace untuk menyemak sama ada aksara ialah rentetan, nombor atau aksara khas. Gunakan fungsi isLetter untuk menyemak rentetan, fungsi isDigit untuk menyemak nombor dan gabungan fungsi isLetter, isDigit dan isWhitespace untuk menyemak aksara khas.
Berikut ialah sintaks fungsi rentetan
Character.isLetter(ob1)
Berikut ialah sintaks fungsi berangka
Character.isDigit(ob1)
Berikut ialah sintaks fungsi rentetan
(!Character.isDigit(ob1)&& !Character.isLetter(ob1)&& !Character.isWhitespace(ob1))
Langkah-1 − Minta pengguna memasukkan aksara yang diperlukan.
Langkah 2 - Tunjukkan menu.
Langkah 3 - Minta pengguna memasukkan pilihan mereka.
Langkah-4 - Gunakan kotak suis untuk pergi ke pemilihan dan lakukan tindakan.
Langkah 5 - Cetak hasilnya.
Mari lihat program untuk memahaminya dengan jelas.
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner( System.in ); System.out.println("Enter a character to check if it's a Number, String or a Special character"); char ob1 = sc.next().charAt(0); System.out.println("Now choose the operation you want to perform from the menu given below. "); mainLoop: while (true) { Scanner inn = new Scanner( System.in ); System.out.println("\n***Menu***"); System.out.println("1. Check if a character is number"); System.out.println("2. Check if a character is String"); System.out.println("3. Check if a character is Special character"); System.out.println("4. Terminate the program"); System.out.println("Enter action number (1-4): "); int command; if ( inn.hasNextInt() ) { command = inn.nextInt(); inn.nextLine(); } else { System.out.println("\nILLEGAL RESPONSE. YOU MUST ENTER A NUMBER."); inn.nextLine(); continue; } switch(command) { case 1: if (Character.isDigit(ob1)) { System.out.println("Character is a number!"); } else { System.out.println("Character is not a number!"); } break; case 2: if (Character.isLetter(ob1)) { System.out.println("Character is a String!"); } else { System.out.println("Character is not a String!"); } break; case 3: if (!Character.isDigit(ob1) && !Character.isLetter(ob1) && !Character.isWhitespace(ob1)) { System.out.println("Character is a Special Character!"); } else { System.out.println("Character is not a Special Character!"); } break; case 4: System.out.println("Program terminated"); break mainLoop; default: System.out.println("Wrong choice!!"); } } } }
Enter a character to check if it's a Number, String or a Special character t Now choose the operation you want to perform from the menu given below. ***Menu*** 1. Check if a character is number 2. Check if a character is String 3. Check if a character is Special character 4. Terminate the program Enter action number (1-4): 1 Character is not a number! ***Menu*** 1. Check if a character is number 2. Check if a character is String 3. Check if a character is Special character 4. Terminate the program Enter action number (1-4): 3 Character is not a Special Character! ***Menu*** 1. Check if a character is number 2. Check if a character is String 3. Check if a character is Special character 4. Terminate the program Enter action number (1-4): 2 Character is a String! ***Menu*** 1. Check if a character is number 2. Check if a character is String 3. Check if a character is Special character 4. Terminate the program Enter action number (1-4): $ ILLEGAL RESPONSE. YOU MUST ENTER A NUMBER. ***Menu*** 1. Check if a character is number 2. Check if a character is String 3. Check if a character is Special character 4. Terminate the program Enter action number (1-4): 1 Character is not a number! ***Menu*** 1. Check if a character is number 2. Check if a character is String 3. Check if a character is Special character 4. Terminate the program Enter action number (1-4): 3 Character is not a Special Character! ***Menu*** 1. Check if a character is number 2. Check if a character is String 3. Check if a character is Special character 4. Terminate the program Enter action number (1-4): 2 Character is a String! ***Menu*** 1. Check if a character is number 2. Check if a character is String 3. Check if a character is Special character 4. Terminate the program Enter action number (1-4): 4 Program terminated
Dalam artikel ini, kami meneroka cara menyemak sama ada aksara ialah rentetan, nombor atau aksara khas dalam Java dengan menggunakan pendekatan dipacu menu.
Atas ialah kandungan terperinci Pemacu menu JAVA untuk menyemak sama ada aksara ialah rentetan, nombor atau aksara khas. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!