密碼檢查程序應該輸入用戶名和密碼,並輸出密碼是否有效。Java密碼模式
我一直在嘗試使用正則表達式,但我有一個問題。該模式適用於我所有的規則,但一個是用戶名規則。 此外,有沒有辦法將輸出從「真」或「假」更改爲自定義?
我迄今爲止代碼:
import java.util.regex.*;
import java.util.Scanner;
public class validPassword {
private static Scanner scnr;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Variable Management
String un, pw, req; // Variable Variable
System.out.println("Please enter a username: ");
// ^Need to implement so if it matches the password it's invalid^
un = input.nextLine(); // Gathers user's username input
System.out.println("Please enter a password: ");
pw = input.nextLine(); // Gathers user's password input
req = "(?=.*[0-9])(?=.*[a-zA-Z]).{8,}";
System.out.println(pw.matches(req)); // Can I customize the output?
}
}
我明白任何幫助! :)
你知道,與上次編輯你改變了從「必須包含數字,小寫字母和大寫字母」到「必須包含的意義數字和小寫**或**大寫字母「?現在只需使用小寫字母和大寫字母即可。你是否對我的答案失去了贊同?如果是這樣,爲什麼? – Vampire