2013-03-19 136 views
0

我遇到了程序输出的麻烦。我已经开发了类GetInput作为构造函数,在询问各种输入问题时我可以重用。每个被问到的问题都需要等于或大于最小值/小于传递给类/构造函数的最大值。我遇到的问题是,while循环运行时,在最终返回正确的值之前,它要求输入四次。使用嵌套if语句在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 
    } 
} 
+0

尝试使用Eclipse调试器和理解你的程序 – 2013-03-19 11:14:18

回答

2

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

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

它没有循环4次,但每当你说inputFromUser.hasNextInt()或扫描仪实际上会阻止您等待您输入值。

因此,这是很明显,你必须修复

+0

感谢您的快速回复。有关如何解决此错误的任何建议?当你说进一步的投入被封锁时,这到底意味着什么? – itzkreator 2013-03-19 11:20:11

+0

这意味着你的程序将停止执行,并等待数据在数据流上可用(即:等到你按下控制台上的某个东西并按回车键) – gerrytan 2013-03-19 11:25:53

+0

啊,我现在明白了,尽管对我来说没什么意义。我输入后应该关闭扫描仪吗?或者也许还有另一种说法可以用来代替?我是编程新手,只是刚刚开始学习Java,所以如果我很难解释,很抱歉。 – itzkreator 2013-03-19 15:25:44

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 
    } 
+0

几乎可以修复它,它只会迭代两次,而不是四次。 – itzkreator 2013-03-19 11:39:14

+0

这取决于你输入的值是什么?什么是输入? – 2013-03-19 11:43:19

+0

那么第一个问题的理想输入是1或者0。他们两个都会导致程序达到我放入的'第二次'标志,但不会进一步。当我再次按Enter键时,它按照要求完成循环 – itzkreator 2013-03-19 13:04:24