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 = - 给定值不是数字!
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短