使用嵌套if语句在while循环中循环的问题

时间:2013-03-19 11:08:50

标签: java eclipse

我的程序输出有问题。我已经开发了类'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
    }
}

2 个答案:

答案 0 :(得分:2)

从手册页http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#hasNextLong()

  

hasNext和next方法都可能阻止等待进一步输入

它没有循环4次,但只要您说inputFromUser.hasNextInt()inputFromUser.nextInt(),扫描程序就会阻止您等待输入值。

所以这显然是你必须解决的错误

答案 1 :(得分:0)

您应该将输入存储在某个变量中,然后在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
    }