2013-03-19 211 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