ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java在“无效输入”上循环切换案例语句

2019-11-01 17:12:33  阅读:150  来源: 互联网

标签:loops switch-statement java


如果用户输入未定义选项的任何内容,我想更新代码以循环我的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");

}

在这方面的任何帮助将不胜感激.

解决方法:

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

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

标签:loops,switch-statement,java
来源: https://codeday.me/bug/20191101/1985256.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有