以下是 Java 代码实现:

public class TelephoneCheck {
   public static boolean CheckTel(String rc, String n) {
      // 判断地区码是否合法
      if (rc.length() == 3 || rc.length() == 4) {
         try {
            int rcNum = Integer.parseInt(rc);
            if (rcNum == 0) {
               return false;
            }
         } catch (NumberFormatException e) {
            return false;
         }
      } else {
         return false;
      }
      
      // 判断电话号码是否合法
      if (n.length() == 7 || n.length() == 8) {
         try {
            int nNum = Integer.parseInt(n);
            if (nNum < 2000000) {
               return false;
            }
         } catch (NumberFormatException e) {
            return false;
         }
      } else {
         return false;
      }
      
      return true;
   }
   
   public static void main(String[] args) {
      String rc = '010';
      String n = '12345678';
      boolean result = CheckTel(rc, n);
      System.out.println(result);
   }
}

在 main 函数中,用例子输入地区码和电话号码,然后调用 CheckTel 函数进行检查,最后输出检查结果。

Java 电话号码有效性检查程序 - 中国固定电话号码格式

原文地址: https://www.cveoy.top/t/topic/noLA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录