的提示是:JAVA:密碼標準
寫Java程序在命令行中讀取來自用戶的字符串(密碼),然後檢查密碼符合公司的密碼策略。
策略是:
1)密碼必須至少8個字符
2)密碼必須包含一個大寫字母
3)密碼必須包含一個數位。
使用while循環步驟通過串。
輸出「密碼OK」如果密碼相符,否則
輸出「密碼不符合政策。」
我有這樣下來,這似乎不是還在正常工作:
String password = "";
boolean hasDigitAndUpper = false;
boolean hasUpper = false;
int i = 0;
System.out.print("Please enter a password: ");
password = scnr.nextLine();
if (Character.isUppercase(password.charAt(i))){
if (Character.isUppercase(password.charAt(i))) {
i++;
hasDigitAndUpper = true;
}
}
while (password.length() < 8 || hasDigitAndUpper != true) {
System.out.println("Password does not conform to policy");
System.out.print("Please enter a password: ");
password = scnr.nextLine();
}
System.out.println("Password OK");
我想我最大的問題是我的布爾值,我無法弄清楚如何解決。
彈出的東西:一旦你得到一個新的密碼,你不要重新編譯hasDigitAndUpper。 Java不會自動讓它保持最新狀態。 – yshavit
啊。我對編碼相當陌生,所以我不知道這一點。謝謝yshavit! GhostCat:我一整天都在工作和上課,現在就回到這個問題。大聲笑。 – Gene