Java程序检查字符是否为ASCII 7位数字
要检查输入的值是否为ASCII7位数字,请检查从'0'到'9'的字符。
在这里,我们有一个数字字符。
char one = '9';
现在,我们检查了if-else条件是否是数字字符,从'0'到'9'
if (c >= '0' && c <= '9') {
System.out.println("给定值是数字!");
} else {
System.out.println("给定值不是数字!");
}示例
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("给定值是数字!");
} else {
System.out.println("给定值不是数字!");
}
}
}输出结果
Given value = 9 给定值是数字!
让我们看看另一个示例,其中我们检查数字字符。但是,此处要检查的给定值不是数字。
示例
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("给定值是数字!");
} else {
System.out.println("给定值不是数字!");
}
}
}输出结果
Given value = s 给定值不是数字!
现在让我们再看一个示例,其中我们检查数字字符。但是,此处要检查的给定值不是数字。
示例
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("给定值是数字!");
} else {
System.out.println("给定值不是数字!");
}
}
}输出结果
Given value = - 给定值不是数字!