我的循環似乎只循環一次,我找不到原因。我嘗試過不同的循環,但是無濟於事。請幫忙。爲什麼我的while循環只循環一次
applyPin方法首先顯示3次嘗試,然後2次嘗試但隨後繼續顯示2次嘗試,爲什麼?
public class Transactions {
private static final int MAX_PIN_TRIES = 3;
private int pin = 1234;
private int misses;
public boolean applyPin(int pinNumbers){
misses = 0;
boolean isCorrect = pinNumbers != pin;
while(misses <= MAX_PIN_TRIES){
if(isCorrect){//if pin entered does not match pin set
misses++;
throw new IllegalArgumentException("Your pin is incorrect");
}
else if (!isCorrect){
System.out.println("Your pin has been accepted");
}//end if
else{
System.out.printf("Your have failed to enter the correct pin %s times. You cannot access the ATM.",
MAX_PIN_TRIES);
}
}
return isCorrect;
}
public int getRemainingTries(){
return MAX_PIN_TRIES - misses;
}
}
提示器類,其中appliedPin方法是從所謂:
public class Prompter {
private Transactions transactions;
public Prompter (Transactions transactions){
this.transactions = transactions;
}
public boolean promptForPin(){
Scanner scanner = new Scanner(System.in);
//prompt the user
boolean isMatch = false; //is pin a match?
boolean isAcceptable = false; //is value acceptable? Set it to default to false
do{
System.out.print("Enter your pin: ");
int pinEntered = scanner.nextInt();// gets the inputs
try{
isMatch = transactions.applyPin(pinEntered);
isAcceptable = true;
}
catch(IllegalArgumentException iae){
System.out.printf("%s. Please try again \n",iae.getMessage());
displayProgress();
}
}
while (! isAcceptable);
return isMatch;
}
public void displayProgress(){
System.out.printf("You have %s tries to enter the correct PIN \n",
transactions.getRemainingTries());
}
}
主要方法:
public class Banking {
public static void main(String[] args) {
Transactions transactions = new Transactions();
Prompter prompter = new Prompter(transactions);
prompter.displayProgress();
prompter.promptForPin();
}
}
你通過與調試器單步執行代碼的代碼? –