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 = - 给定值不是数字!
热门推荐
5 天仙配简短祝福语
10 给妈妈祝福语简短内容
11 对女儿生日祝福语简短
12 结婚伴娘祝福语简短精辟
13 生日贺卡祝福语简短创意
14 考上大学简短祝福语、
15 爸妈金婚文案祝福语简短
16 简短搞笑蛋糕生日祝福语
17 中考祝福语给同学简短
18 婚礼酒祝福语简短大气