JAVA语言正则表达式实现密码规则设置

来源:互联网 发布:淘宝新势力周时间表 编辑:程序博客网 时间:2024/06/17 05:45
<span style="font-size:18px;">密码规则:长度不能小于6位,必须包含字母和数字。</span>
public void say() {Scanner sc = new Scanner(System.in);String password = "";int i = 0;while (i < 3) {System.out.println("请输入你的密码:");password = sc.nextLine();if (password.length() < 6) {System.out.println("您的密码长度小于6");} else {boolean boo1 = password.matches("\\d*[a-z]+\\d*");// 正则表达式boolean boo2 = password.matches("[a-z]*\\d+[a-z]*");if (boo1 & boo2) {System.out.println("您的密码设置成功");break;}}/* * if (containsLetter(password) && containsNumber(password)) { * System.out.println("你的密码保存成功"); break; *  * } } */if (i == 2) {System.out.println("对不起,三次输入已完结!");return;}i++;}System.out.println("你的密码是:" + password + "");}/* * private boolean containsNumber(String str) { String number = * "1234567890"; for (int i = 0; i < str.length(); i++) { char c = * str.charAt(i); if (number.contains("" + c)) { return true; } } *  * return false; } *  * private boolean containsLetter(String str) { String letter = * "qwertyuioplkjhgfdsazxcvbnm"; for (int i = 0; i < str.length(); i++) { * char c = str.charAt(i); if (letter.contains("" + c)) { return true; } } * return false; } */}

0 0
原创粉丝点击