我想寫一個程序,用於檢查插入密碼:計劃密碼驗證
- 長度是8
- 至少1個大寫字母
- 至少1個小寫字母的最小
- 至少3位數字
我寫了這個程序,但它沒有給我正確的輸出:
import java.util.Scanner;
public class Question5 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.println("Please enter your password: ");
String input = in.nextLine();
boolean flag = validate(input);
if (flag = true) {
System.out.println("password verified");
}
else {
System.out.println("not a good password");
}
}
public static boolean validate(String input) {
boolean flag = false;
int uppercaseCounter = 0;
int lowercaseCounter = 0;
int digitCounter = 0;
int letterCounter = 0;
for (int i = 0; i<(input.length()); i++) {
int totalCounter = digitCounter + letterCounter;
if (totalCounter >= 8 && digitCounter >= 3 && uppercaseCounter > 0 && lowercaseCounter > 0) {
flag = true;
}
else {
if (Character.isDigit(i)) {
digitCounter++;
}
if (Character.isLetter(i)) {
letterCounter++;
}
if (Character.isUpperCase(i)) {
uppercaseCounter++;
}
if (Character.isLowerCase(i)) {
lowercaseCounter++;
}
}
}
return flag;
}
}
有人可以幫助我嗎?非常感謝你!
'if(flag == true)'notice it should be double'=='! – TDG
@TDG或者,更好的是,只需'if(flag)'。 – jsheeran
什麼是輸入和輸出呢? –