为什么我的while循环只循环一次

问题描述:

我的循环似乎只循环一次,我找不到原因。我尝试过不同的循环,但是无济于事。请帮忙。为什么我的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

你通过与调试器单步执行代码的代码? –

其实,你的循环将继续永远如果正确的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