JAVA語言正則表示式實現密碼規則設定
阿新 • • 發佈:2019-01-08
<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; } */ }