• 【典型案例】验证号码


    //通过正则表达式,验证输入内容是否格式正确

    public class Test {    
        public static void main(String[] args) {
            Scanner input=new Scanner(System.in);
            System.out.println("请输入手机号:");
            String tel=input.next();
            if(!tel.matches("1[1-9]\\d{9}")) {
                System.out.println("手机号不合法");
            }else {
                System.out.println("请输入密码:");
                String password=input.next();
                if(!password.matches("\\w{6,12}")) {
                    System.out.println("密码不合法");                
                }else {
                    System.out.println("请输入邮箱:");
                    String email=input.next();
                    if(!email.matches("\\w+[@]\\w+[.]\\w+")) {
                        System.out.println("邮箱不合法");
                    }else {
                        System.out.println("请输入年龄:");
                        try {
                            int age=input.nextInt();
                            if(age<0||age>150) {
                                System.out.println("年龄不合法");
                            }else {
                                System.out.println("请输入固定电话号码:");
                                String phone=input.next();
                                if(!phone.matches("\\d{3}||\\d{4}[-]\\d+")) {
                                    System.out.println("固定电话不合法");
                                }
                            }
                        }catch(Exception e) {
                            System.out.println("年龄输入不合法");
                        }
                    }
                } 
            }
            
        }
    }

  • 相关阅读:
    【Git】Git 的基本操作 -- 详解
    日志11.7学习Http请求响应协议及语法、编写接口测试
    LeetCode_二分搜索_中等_162.寻找峰值
    Spring中事务的几种失效场景
    湖仓一体方案有很多,为何偶数的实时湖仓脱颖而出?
    超实用!推荐5款办公黑科技软件,用了就离不开
    gitlab登录出现的Invalid login or password问题
    93-Java的字符缓冲流、案例:出师表
    Python:求求按规范写我
    矩阵分析与应用(14)
  • 原文地址:https://blog.csdn.net/jeremy9627/article/details/125510608