检查字符是否是 ASCII 7 位数字的 Java 程序


若要检查输入值是否是 ASCII 7 位数字,请检查字符从“0”到“9”。

这里,我们使用数值字符。

char one = '9';

现在,我们使用 if-else 条件检查了从“0”到“9”的数字字符

if (c >= '0' && c <= '9') {
   System.out.println("Given value is numeric!");
} else {
   System.out.println("Given value is not numeric!");
}

示例

 实时演示

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("Given value is numeric!");
      } else {
         System.out.println("Given value is not numeric!");
      }
   }
}

输出

Given value = 9
Given value is numeric!

让我们看另一个检查数字字符的示例。但是,这里给定要检查的值不是数字。

示例

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("Given value is numeric!");
      } else {
         System.out.println("Given value is not numeric!");
      }
   }
}

输出

Given value = s
Given value is not numeric!

现在让我们看另一个示例,其中我们检查数字字符。但是,这里给定要检查的值不是数字。

示例

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("Given value is numeric!");
      } else {
         System.out.println("Given value is not numeric!");
      }
   }
}

输出

Given value = -
Given value is not numeric!

更新日期:25-Jun-2020

151 次浏览

开启职业生涯

完成课程获得认证

开始使用
广告
© . All rights reserved.