简体   繁体   English

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

[英]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? 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;
      }
 }

The prompter class in which the appliedPin method is called from: 在以下提示类中调用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());

      }

}

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. 实际上,如果输入正确的Pin,您的循环将永远继续下去。

This is because when the correct PIN is entered, isCorrect becomes false and the second branch of the if statement is reached: 这是因为当输入正确的PIN时, isCorrect变为false,并且到达了if语句的第二个分支:

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. 打印后,由于misses次数小于3,因此循环开始新的迭代。它再次通过第二个分支并开始新的迭代。 Since you did not do anything to misses . 既然你没有做任何misses事情。

What I think you should do in this branch is just to return true; 我认为您应该在此分支中做的只是return true; after printing the message. 打印消息后。

In the first branch of the if statement, don't throw an exception. 在if语句的第一个分支中,不要引发异常。 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. 由于isCorrect为true或不为true,因此永远不会到达if语句的第三个分支。 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());

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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