2012-02-23 5 views
3

ユーザーがオプションを定義していないものを入力した場合、swtichステートメントをループするようにコードを更新しようとしています。私は様々な検索用語からの戻りが近くに来たが、これまでのところ運がないという多数のページを精査した。 ここに私のコードは、それに刺すようなものを取得したいものを取得する必要があります。Javaは "無効な入力"のswitch-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 –

答えて

1

あなたは非常に奇妙な方法で実行変数を使用しています。ループの最後に実行をfalseに設定するので、ループは繰り返されません。有効なオプションをrun=falseに設定して間違ったオプションを入力すると、ループがもう一度実行されます。

は、 switch文の末尾に run=falseを削除する「実行= false」の部分は、適切な場所ではありません System.out.println("OptionX");

+0

迅速な対応に感謝します。オンラインコミュニティは大きな助けになっています。 –

2

後にそれを追加し、それが(有効かどうか)どんな答えを実行しています。

"run = false;"に移動する必要があります。有効な各case文の内部、のような:ところで

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

:「しばらく(実行==真)」

+0

元の実装にはwhile(run) 投票が可能で、2つの回答がある場合はそうです。 –

1

問題はその実行した後である「(実行)しながら、」あなたが書くことができ、冗長ですステートメントは、デフォルトでブール値の実行をfalseに設定し、ループから出てきます。必要なのはこれをスキップする方法です。 -

run = false; 

代わりにループ状態に直接移行します。 1つの解決策は、デフォルトで 'continue'ステートメントを追加することです。 -

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

コードの変更は最小限に抑えられていますが、このような小規模な実装では良い選択です。しかし、2つのwhileループが存在する大規模な環境では、最終的にelseで終わるいくつかのifループがあり、それがスイッチにつながります。内側のwhileループの上にループします。ループは、ループの入力だけでなくすべての入力を再度促します。スイッチケース –

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 

} 
関連する問題