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 婆婆高寿祝福语大全简短