簡體   English   中英

使用嵌套if語句在while循環中循環的問題

[英]Problems looping in a while loop with nested if statements

我的程序輸出有問題。 我已經開發了類'GetInput'作為構造函數,我可以在詢問各種輸入問題時重用它。 提出的每個問題都需要等於或大於最小/小於傳遞給類/構造函數的最大數。 我遇到的問題是,當運行while循環時,它會在最終返回正確的值之前要求輸入四次。

我添加了標志,這些標志在我們顯示時已經解決了。 第一次添加輸入后的第一個顯示。 然后第二次,然后第四次。 第四次它還顯示我希望它在一次迭代中到達的標志“結束”。 為什么在最終正確返回值之前循環四次?

非常感謝提前。 這只是我學習java的第二天,這讓我瘋了。

import java.util.Scanner; //Import the scanner class

public class main {
public static void main(String[] args) {

    //Set variables to hold the entry cost for each category
    int costAccChild = 2;
    int costUnaccChild = 5;
    int costAdult = 10;
    int costSenior = 8;

    int test = GetInput(0, 1, "Enter a group? (Yes=1/No=0)");
    System.out.println(test);
    System.out.println("the end");

}

 static int GetInput(int min, int max, String request){     
    boolean inputValid = false; //Sets default value to variable for while loop
    int userInput = 0; //Sets default variable for input return

    while (inputValid == false) { //Loops until receives correct input
        System.out.println(request); //Prints question asking for input
        Scanner inputFromUser = new Scanner(System.in); //Ask user for input
        System.out.print("First time"); //FLAG DISPLAYS AFTER FIRST SCANNER

        if (inputFromUser.hasNextInt() == true){ //Check if input has an integer

            System.out.print("Second Time"); //FLAG SHOWS AFTER SECOND SCANNER

            if (inputFromUser.nextInt() >= min && inputFromUser.nextInt() <= max ){ //Check if input is valid
                userInput = inputFromUser.nextInt();
                inputValid= true;

                System.out.print("Fourth time"); //FLAG WORKS FORTH TIME

            }else{ //Input is not correct integer, give error message
                System.out.println("Input is not valid");           
                }   

        }else{ //Input is not an integer at all, give error message
            System.out.println("Input is not valid");
        }
    }
    return userInput; //Returns valid input
    }
}

從手冊頁http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#hasNextLong()

hasNext和next方法都可能阻止等待進一步輸入

它沒有循環4次,但每當你說inputFromUser.hasNextInt()inputFromUser.nextInt() ,掃描器實際上阻止等你輸入一個值。

所以這顯然是你必須修復的錯誤

您應該將輸入存儲在某個變量中,然后在if條件下進行比較。

這不會阻止輸入以進一步輸入。

嘗試這個:

public static void main(String[] args) {

    //Set variables to hold the entry cost for each category
    int costAccChild = 2;
    int costUnaccChild = 5;
    int costAdult = 10;
    int costSenior = 8;

    int test = GetInput(0, 1, "Enter a group? (Yes=1/No=0)");
    System.out.println("Return Result: " + test);
    System.out.println("The end");

}

static int GetInput(int min, int max, String request) {
        boolean inputValid = false; //Sets default value to variable for while loop
        int userInputMin = 0, userInputMax=0; //Sets default variable for input return

        while (inputValid == false) { //Loops until receives correct input
            System.out.println(request); //Prints question asking for input
            Scanner inputFromUser = new Scanner(System.in); //Ask user for input
            System.out.print("First time: "); //FLAG DISPLAYS AFTER FIRST SCANNER

            if (inputFromUser.hasNextInt() == true) { //Check if input has an integer
                userInputMin = inputFromUser.nextInt();
                System.out.print("Second Time: "); //FLAG SHOWS AFTER SECOND SCANNER
                if (inputFromUser.hasNextInt() == true) { //Check if input has an integer
                    userInputMax = inputFromUser.nextInt();
                    if (userInputMin >= min && userInputMax <= max) { //Check if input is valid

                        inputValid = true;

                        System.out.println("Third time"); //FLAG WORKS Third Time

                    } else { //Input is not correct integer, give error message
                        System.out.println("Input is not valid");
                    }
                }    
            } else { //Input is not an integer at all, give error message
                System.out.println("Input is not valid");
            }
        }
        return userInputMin; //Returns valid input
    }  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM