繁体   English   中英

Java循环关于“无效输入”的switch-case语句

[英]Java Looping a switch-case statement on “invalid input”

我希望更新我的代码以循环我的swtich语句,如果用户输入任何没有定义选项的东西。 我已经搜索了从各种搜索条件返回的众多页面并且已经接近但是到目前为止还没有运气。 我的代码应该得到任何想要抓住它的人。

 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");

}

对此的任何帮助将不胜感激。

您可以在循环中添加标签。 这样,当你得到正确的选项时,你将退出循环。

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

}

您正以非常奇怪的方式使用run变量。 由于您在循环结束时将run设置为false ,因此循环将永远不会重复。 如果更改它以便只有有效选项设置为run=false ,则输入错误的选项将导致循环再次运行。

switch语句的末尾删除run=false ,在System.out.println("OptionX");之后添加System.out.println("OptionX");

“run = false”部分不在正确的位置,无论答案是什么(有效与否)都会执行。

你应该移动“run = false;” 在每个有效的case语句中,如:

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

顺便说一句:“while(run == true)”是多余的,你可以写“while(run)”

问题是在默认情况下执行语句后,它将布尔运行设置为false,从而退出循环。 我们需要的是一种跳过这种方式的方法: -

run = false;

而是直接进入循环条件。 一种解决方案是在默认情况下添加'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

   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM