Java程式檢查輸入的字元是否為ASCII 7位數字和字元


檢查輸入的值是否為ASCII 7位數字和字元(字母數字),檢查字元的ASCII值是否存在 −

A to Z
Or
a to z
Or
0 to 9

在此,我們有以下值 −

char one = '5';

現在,我們已經使用if-else對ASCII 7位數字和字元檢查了一些條件。

if ((one >= 'A' && one <= 'Z') || (one >= 'a' && one <= 'z') || (one >= '0' && one <= '9')) {
System.out.println("Given value is numeric and character (alphanumeric)!");
} else {
System.out.println("Given value is not numeric and character!");
}

示例

 即時演示

public class Demo {
public static void main(String []args) {
   char one = '5';
   if ((one >= 'A' && one <= 'Z') || (one >= 'a' && one <= 'z') || (one >= '0' && one <= '9')) {
         System.out.println("Given value is numeric and character (alphanumeric)!");
      } else {
         System.out.println("Given value is not numeric and character!");
      }
   }
}

輸出

Given value is numeric and character (alphanumeric)!

讓我們看另一個示例 −

示例

 即時演示

public class Demo {
public static void main(String []args) {
   char one = 'V';
   if ((one >= 'A' && one <= 'Z') || (one >= 'a' && one <= 'z') || (one >= '0' && one <= '9')) {
         System.out.println("Given value is numeric and character (alphanumeric)!");
      } else {
         System.out.println("Given value is not numeric and character!");
      }
   }
}

輸出

Given value is numeric and character (alphanumeric)!

更新於: 2020年6月25日

108次觀看

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.