2012-11-23 109 views
2

我已經創建了我的菜單do〜while(true);但每次用戶插入一個號碼,而不是運行程序,它就會再次顯示菜單!你怎麼看?做循環顯示菜單

//我的主要方法

public static void main(String[] args) { 

    DataReader reader = new DataReader(); // The reader is used to read data from a file 

    // Load data from the file 
    if(reader.loadData(args[0])) {   // The filename is entered using a command-line argument 

     vehicles= reader.getVehicleData(); // Store the arrays of Vehicle 

     // Display how many shapes were read from the file 
     System.out.println("Successfully loaded " + vehicles[0].getCount() + 
          " vehicles from the selected data file!"); 
     displayMenu(); 
    } 
} 

// dispaly菜單方法

private static void displayMenu() { 
    Scanner input = new Scanner(System.in); 

    do { 
     System.out.println("\n\n   Car Sales Menu"); 
     System.out.println("--------------------------------------"); 
     System.out.println("1 - Sort vehicles by owner's Last Name"); 
     System.out.println("2 - Sort vehicles by vehicle Model"); 
     System.out.println("3 - Sort vehicles by vehicle Cost\n"); 
     System.out.println("4 - List All Vehicles"); 
     System.out.println("5 - List All Cars"); 
     System.out.println("6 - List American Cars Only (Formal)"); 
     System.out.println("7 - List Foreign Cars only (Formal)"); 
     System.out.println("8 - List All Trucks"); 
     System.out.println("9 - List All Bicycles"); 
     System.out.print("\nSelect a Menu Option: "); 
     getInput(input.next()); // Get user input from the keyboard 
    } 
    while(true); // Display the menu until the user closes the program 
} 

// getInput方法

private static void getInput(String input) { 
    switch(Convert.toInteger(input)) { 
     case 1: // Sort Vehicles by Owner's Last Name 
      Array.sortByOwnerName(vehicles); 
      break; 
     case 2: // Sort Vehicles by Vehicle Make & Model 
      Array.sortByVehicleMakeModel(vehicles); 
      break; 
     case 3: // Sort Vehicles by Vehicle Cost 
      Array.sortByVehicleCost(vehicles); 
      break; 
     case 4: // List All Vehicles 
      displayVehicleData(0); 
      break; 

     default: 
      System.out.print("The entered value is unrecognized!"); 
      break; 
    } 
} 
+0

getInput()方法在哪裏? – asteri

+0

@Jeff我剛更新了我的問題。 – Dani

+1

你的getInput()方法根本沒有意義。你在做什麼? –

回答

2

因爲你有while(true);,這意味着菜單它會在一個無限循環,直到破則調用。

試着這樣做:

do { 
     System.out.println("\n\n   Car Sales Menu"); 
     System.out.println("--------------------------------------"); 
     System.out.println("1 - Sort vehicles by owner's Last Name"); 
     System.out.println("2 - Sort vehicles by vehicle Model"); 
     System.out.println("3 - Sort vehicles by vehicle Cost\n"); 
     System.out.println("4 - List All Vehicles"); 
     System.out.println("5 - List All Cars"); 
     System.out.println("6 - List American Cars Only (Formal)"); 
     System.out.println("7 - List Foreign Cars only (Formal)"); 
     System.out.println("8 - List All Trucks"); 
     System.out.println("9 - List All Bicycles"); 
     System.out.print("\nSelect a Menu Option: "); 
    try { 
     int input = Integer.parseInt(getInput(input.next())); // Get user input from the keyboard 


     switch (input) { 
     case 1: // do something 
       break; 
     case 2: // do something 
       break; 
     ... 
     } 
     } catch (NumberFormatException e) { ... } 

    } 
    while(true); // Display the menu until the user closes the program 

您可以使用switch處理輸入,並根據輸入做相應的動作。

+0

我將其更改爲false,但不顯示結果! – Dani

+1

如果您輸入false,則意味着循環將只執行一次。 – dreamcrash

+1

@ user1834534嘗試使用開關來處理您插入的輸入 – dreamcrash

2
while(true); // Display the menu until the user closes the program 

while true並不意味着完全的事你寫了我n評論。您需要在while循環中添加一些其他條件來檢查該條件。該條件應該在您從用戶處讀取的input上。

對於例如,就像這樣。請注意,這可能不能完全解決你的問題,似乎有其他一些問題也與您的代碼: -

int userInput = 0; 
do { 
    try { 
     userInput = Integer.parseInt(getInput(input.next())); 
    } catch (NumberFormatException e) { 
     userInput = 0; 
    } 

} while (userInput < 1 || userInput > 9); 

return userInput; // For this you need to change return type of `displayMenu()` 

然後,過程中你main()方法返回的userInput。在那裏你需要將返回值存儲在某個局部變量中。

int userInput = displayMenu(); 
+0

好吧,我正在更換...謝謝 – Dani

1

因爲你的while循環是while(true)它會一直保持循環,直到程序被強行破壞。沒有你的getInput()函數的內容,所有可以說的是循環永遠不會結束。

您需要處理用戶的輸入,或者在getInput()方法中或使用後輸入,然後在滿足特定條件時有條件地跳出while(true)

1

假設你getInput方法做什麼它說,確實如此,你還沒有真正與你的輸入做任何事情一旦被讀入。

因此,當用戶輸入一個值,你的程序讀取值,高興地忽略它,然後再次運行菜單。

+0

更有意義...讓我檢查一下 – Dani