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

时间:2017-04-23 12:46:34

标签: java loops

我的循环似乎只循环了一次,我无法弄清楚原因。我试过不同的循环,为了,做,做,但无济于事。请帮忙。

applyPin方法首先显示3次尝试,然后是2次尝试,但随后继续显示2次尝试,为什么?

output {
stdout { codec => json_lines }
if [type] == "announces" {
    elasticsearch {  
        hosts => "localhost:9200"
        document_id => "%{job_id}"
        index => "dooone"
    document_type => "%{type}"
    }
} else {
    elasticsearch {  
        hosts => "localhost:9200"
    document_id => "%{uid}"
    index => "dootwo"
    document_type => "%{type}"
    }
}
}

从中调用appliedPin方法的提示符类:

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

主要方法:

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

      }

}

1 个答案:

答案 0 :(得分:0)

实际上,如果输入了正确的Pin,你的循环将永远继续。

这是因为输入正确的PIN后,isCorrect变为false并且达到了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());

    }

}