JAVA菜单驱动程序,用于检查字符是字符串、数字还是特殊字符

JAVA菜单驱动程序,用于检查字符是字符串、数字还是特殊字符

在本文中,我们将看到一个使用Java编程语言实现的菜单驱动程序,用于检查输入的字符是数字、字符串还是特殊字符。我们将使用switch case来实现这个应用程序。

向您展示一些实例

实例-1

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

Instance-2

的中文翻译为:

实例-2

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

Instance-3

的中文翻译为:

实例-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 步 - 要求用户输入他们的选择。

Step-4 - 使用开关盒转到选择并执行操作。

第 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中检查一个字符是否为字符串、数字或特殊字符。

以上就是JAVA菜单驱动程序,用于检查字符是字符串、数字还是特殊字符的详细内容,更多请关注其它相关文章!