檢查字元是否是 ASCII 7 位數字的 Java 程式
若要檢查輸入值是否是 ASCII 7 位數字,請檢查字元從“0”到“9”。
這裡,我們使用數值字元。
char one = '9';
現在,我們使用 if-else 條件檢查了從“0”到“9”的數字字元
if (c >= '0' && c <= '9') { System.out.println("Given value is numeric!"); } else { System.out.println("Given value is not numeric!"); }
示例
public class Demo { public static void main(String []args) { char c = '9'; System.out.println("Given value = "+c); if (c >= '0' && c <= '9') { System.out.println("Given value is numeric!"); } else { System.out.println("Given value is not numeric!"); } } }
輸出
Given value = 9 Given value is numeric!
讓我們看另一個檢查數字字元的示例。但是,這裡給定要檢查的值不是數字。
示例
Public class Demo { public static void main(String []args) { char c = 's'; System.out.println("Given value = "+c); if (c >= '0' && c <= '9') { System.out.println("Given value is numeric!"); } else { System.out.println("Given value is not numeric!"); } } }
輸出
Given value = s Given value is not numeric!
現在讓我們看另一個示例,其中我們檢查數字字元。但是,這裡給定要檢查的值不是數字。
示例
public class Demo { public static void main(String []args) { char c = '-'; System.out.println("Given value = "+c); if (c >= '0' && c <= '9') { System.out.println("Given value is numeric!"); } else { System.out.println("Given value is not numeric!"); } } }
輸出
Given value = - Given value is not numeric!
廣告