我需要初始化一個密碼。而密碼不等於保密密碼,並且嘗試少於三次時會詢問用戶密碼。如果密碼正確,打印訪問授權,如果密碼不正確,打印訪問被拒絕,最大嘗試次數達到我需要創建一個密碼檢查器
import java.util.Scanner;
public class PasswordChecker {
public static void main (String [] args) {
Scanner in = new Scanner(System.in);
String secretPassword = "alejandra";
String password;
int attempts = 3;
System.out.println("Please enter password");
password = in.nextLine();
while (!password.equals(secretPassword) && attempts<3)
System.out.println ("incorrect try again");
password = in.nextLine();
if (password.equals (secretPassword)
System.out.println ("access granted");
}
}
我創建了一個無限循環。我是初學者。不知道如何解決它。 – ale
看起來你需要學習使用調試器。請幫助一些[互補調試技術](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之後仍然有問題,請隨時返回更多詳情。 –
[Password Checker]的可能重複(http://stackoverflow.com/questions/34360580/password-checker) – ale