Java 選單驅動程式,用於檢查字元是字串、數字還是特殊字元


在本文中,我們將瞭解一個選單驅動的程式,該程式使用 Java 程式語言 檢查輸入的字元是數字、字串還是特殊字元。我們將使用 switch case 實現此應用程式。

為您展示一些示例

示例 1

Suppose the entered character is ‘a’ then the output should be “Entered character is a String”.

示例 2

Suppose the entered character is ‘1’ then the output should be “Entered character is a number”.

示例 3

Suppose the entered character is ‘$’ then the output should be “Entered character is a Special character”.

語法

要檢查 Java 中的字元是字串、數字還是特殊字元,我們可以使用 isLetter、isDigit 或 isWhitespace 函式。字串由 isLetter 函式檢查,數字由 isDigit 函式檢查,特殊字元由 isLetter、isDigit 和 isWhitespace 函式的組合檢查。

以下是字串函式的語法

Character.isLetter(ob1)

以下是數字函式的語法

Character.isDigit(ob1)

以下是字串函式的語法

(!Character.isDigit(ob1)&& !Character.isLetter(ob1)&& !Character.isWhitespace(ob1))

演算法

步驟 1 - 提示使用者輸入所需的字元。

步驟 2 - 顯示選單。

步驟 3 - 提示使用者輸入他們的選擇。

步驟 4 - 使用 switch case 跳轉到選擇並執行操作。

步驟 5 - 列印結果。

讓我們看看程式以更清楚地瞭解它。

示例

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

在本文中,我們探討了如何使用選單驅動方法在 Java 中檢查字元是字串、數字還是特殊字元。

更新於: 2024 年 5 月 31 日

12K+ 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告