要检查输入的值是否为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("给定的值不是数字和字符!"); }
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 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 numeric and character (alphanumeric)!