2012-02-23 67 views
3

我正在尋找更新我的代碼來循環我的swtich語句,如果用戶輸入任何沒有定義的選項的話。我已經搜遍了各種各樣的搜索術語返回的許多頁面,並且已經接近,但迄今爲止還沒有運氣。 我在這裏的代碼應該得到任何想要刺穿它的人。Java在「無效輸入」上循環切換case語句

java.util.Scanner; 
//import java.lang.Character.*; 
//Thought this was needed to grab single char but its not 
public class caseloop { 
//main Method 
public static void main(String[] args) 
{ 
    Scanner input=new Scanner(System.in); //make so you can give input 
    boolean go = true; // for starting main outer loop 
    boolean run=true; // start inner loop 
    while (go==true) 
    { 
    while (run==true) 
    { 
     //Output 
     System.out.println("Enter option \n 1-Do this \n 2-Do this thing \n 3-Do this other thing"); 
     int option= input.nextInt(); //grab option number 

     switch(option) 
     { 
     /* 
      * This needs to loop and prompt user again if anything other than 1,2, or 3 is entered. 
      */ 
     case 1: 
      System.out.println("Option1"); 
      break; 
     case 2: 
      System.out.println("Option2"); 
      break; 
     case 3: 
      System.out.println("Option3"); 
      break; 
     /*case 4: 
      System.out.println("Option1"); 
      System.out.println("Option2"); 
      System.out.println("Option3"); 

      break; 
     * 
     * 
     * Case 4 was for debug 
     * 
     */ 
     default: 
      System.err.println("Invalid option selected"); 
      /* 
      * On input that is not defined with in the switch-case it will revert to "default" 
      * this fault staement needs to tell ther usere their option is not vaild and then 
      * prompt them to try it again to enter an option. I can not get it to reprompt. 
      * I have tried a while and an if loop both sorta worked but did not actually loop 
      * back to display again. I have been instucted that I am to not use a try catch statment 
      * unless of course that is the only viable option in whichcase I will use it anyways. 
      */ 

      //stupid default statement and its redundent built in "break;" 

     } 
     run=false; 
     } 


    /* 
    * Outer Loop to prompt user if they want to run the entire program again with new entries. 
    */ 
    if (run == false) 
    { 
    System.out.println("Would you like to run again? Y/N"); 
    char again = input.next().charAt(0); 
    again = Character.toUpperCase(again); //force all leters inputed to upper case, lower would work too if i change if conditions 
    if (again == 'Y') 
    { 
     run = true; 
    } 
    else if (again == 'N') 
    { 
     System.out.println("Goodbye."); 
     go=false; 
    } 
    else 
    { 
     System.err.println("Invalid entry. Try again."); 
    } 
    } 
    } 
} 
    //System.err.println("An error occured please try again"); 

} 

任何在這方面的援助將不勝感激。

+0

在這裏進一步測試使用所有建議後,我最終在最終程序中使用了另一個while循環中的兩個方法。感謝大家的幫助。 :D – 2012-02-23 17:12:17

回答

1

您正在以非常奇怪的方式使用運行變量。由於您在循環結束時將運行設置爲false,因此循環將不會重複。如果你改變它,只有有效的選項設置run=false,輸入錯誤的選項會導致循環再次運行。

switch聲明的末尾刪除run=false,則System.out.println("OptionX");

+0

非常感謝您的快速響應。在線社區有很大的幫助。 – 2012-02-23 10:38:05

2

「運行=假」的部分是不是在正確的地方之後添加它,它執行任何答案(有效與否)。

您應該移動「run = false;」每個有效的情況下語句中,如:

case 1: 
     System.out.println("Option1"); 
     run=false; 
     break; 

順便說一句:「雖然(運行==真)」是多餘的,你可以寫「而(運行)」

+0

原始實施已經(運行) 如果我能投票和/或有2個接受的答案,我會。 – 2012-02-23 10:26:03

1

的問題是,執行後語句默認情況下,它將布爾運行設置爲false,並因此出現循環。我們需要的是一種跳過此方法: -

run = false; 

而是直接轉到循環條件。一種解決方案是在默認情況下添加「繼續」語句: -

switch(option) 
    { 
    /* 
     * This needs to loop and prompt user again if anything other than 1,2, or 3 is entered. 
     */ 
    case 1: 
     System.out.println("Option1"); 
     break; 
    case 2: 
     System.out.println("Option2"); 
     break; 
    case 3: 
     System.out.println("Option3"); 
     break; 
    /*case 4: 
     System.out.println("Option1"); 
     System.out.println("Option2"); 
     System.out.println("Option3"); 

     break; 
    * 
    * 
    * Case 4 was for debug 
    * 
    */ 
    default: 
     System.err.println("Invalid option selected"); 
     continue; //this causes control to go back to loop condition 

    } 
+0

一個很好的選擇,因爲它具有最小的代碼更改,但在這樣的小規模實現中,繼續工作很好。然而,在一個更大的環境中有兩個while循環,其中幾個if循環最終以else結尾,導致開關它也循環到內部while循環的頂部,再次提示所有輸入,而不僅僅是輸入開關的情況下 – 2012-02-23 10:35:19

1

您可以將標籤添加到循環中。這樣,當你有正確的選擇時,你將退出循環。

loop: switch(option) 
    { 
/* 
    * This needs to loop and prompt user again if anything other than 1,2, or 3 is entered. 
    */ 
case 1: 
    System.out.println("Option1"); 
    break loop; 
case 2: 
    System.out.println("Option2"); 
    break loop; 
case 3: 
    System.out.println("Option3"); 
    break loop; 
/*case 4: 
    System.out.println("Option1"); 
    System.out.println("Option2"); 
    System.out.println("Option3"); 

    break; 
* 
* 
* Case 4 was for debug 
* 
*/ 
default: 
    System.err.println("Invalid option selected"); 
    continue; //this causes control to go back to loop condition 

}