2017-04-23 155 views
0

我的循環似乎只循環一次,我找不到原因。我嘗試過不同的循環,但是無濟於事。請幫忙。爲什麼我的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(); 
    } 
} 
+2

你通過與調試器單步執行代碼的代碼? –

回答

0

其實,你的循環將繼續永遠如果正確的PIN碼是進入。

這是因爲在輸入正確的PIN碼時,isCorrect爲假,並在到達if語句的第二個分支:

else if (!isCorrect){ 
    System.out.println("Your pin has been accepted"); 
} 

打印後,在循環開始一個新的循環,因爲misses小於3它再次通過第二個分支並開始一個新的迭代。由於您沒有對misses做任何事情。

我認爲你應該在這個分支中做的只是在打印信息後給return true;

在if語句的第一個分支中,不要拋出異常。你不應該因爲用戶輸入不正確而拋出異常。

而if語句的第三個分支將永遠不會到達,因爲isCorrect是真或不真。沒有第三種可能性。

我已經訂好你

class Transactions { 

    private static final int MAX_PIN_TRIES = 3; 
    private int pin = 1234; 
    private int misses = 0; 

    public boolean applyPin(int pinNumbers){ 
     boolean isCorrect = pinNumbers != pin; 
     if (misses < MAX_PIN_TRIES) { 
      if (isCorrect) {//if pin entered does not match pin set 
       misses++; 
       return false; 
      } else { 
       System.out.println("Your pin has been accepted"); 
       return true 
      } 
     } else { 
      System.out.printf("Your have failed to enter the correct pin %s times. You cannot access the ATM.", 
        MAX_PIN_TRIES); 
      System.exit(0); 
     } 
     return false; 
    } 

    public int getRemainingTries(){ 
     return MAX_PIN_TRIES - misses; 
    } 
} 

class Prompter { 
    private Transactions transactions; 

    public Prompter (Transactions transactions){ 
     this.transactions = transactions; 
    } 
    public void 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 

      if(transactions.applyPin(pinEntered)) { 
       isAcceptable = true; 
      } else { 
       System.out.println("Please try again"); 
       displayProgress(); 
      } 
     } 
     while (! isAcceptable); 
    } 

    public void displayProgress(){ 
     System.out.printf("You have %s tries to enter the correct PIN \n", 
       transactions.getRemainingTries()); 

    } 

} 
+0

你清道夫。我現在可以進一步發展 – Kay

+0

@Kay如果您認爲我的答案能解答您的問題,請考慮點擊該選中標記來接受它! – Sweeper