简体   繁体   中英

Why is my while loop looping just once

My loop appears to be looping just once and I can't figure out why. I've tried different loops, for, do, do while but to no avail. Please help.

The applyPin method first displays 3 tries, then 2 tries but then keeps displaying 2 tries, why?

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;
      }
 }

The prompter class in which the appliedPin method is called from:

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());

      }

}

The main method:

public class Banking {

    public static void main(String[] args) {
        Transactions transactions = new Transactions();
        Prompter prompter = new Prompter(transactions);

        prompter.displayProgress();
        prompter.promptForPin();
    }
}

Actually, your loop will continue on forever if the correct Pin is entered.

This is because when the correct PIN is entered, isCorrect becomes false and the second branch of the if statement is reached:

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

After printing, the loop starts a new iteration because misses is less than 3. It goes through the second branch again and starts a new iteration. Since you did not do anything to misses .

What I think you should do in this branch is just to return true; after printing the message.

In the first branch of the if statement, don't throw an exception. You should never throw an exception just because user input is incorrect.

And the third branch of the if statement will never be reached because isCorrect is either true or not true. There is no third possibility.

I have fixed the code for you

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());

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM