Java 在“无效输入”上循环 switch-case 语句
我希望更新我的代码,以便在用户输入任何未定义选项的内容时循环我的 switch 语句。我已经浏览了这里从各种搜索词返回的众多页面,并且已经接近,但到目前为止仍然没有运气。 我这里的代码应该可以让任何想要尝试一下的人知道。
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");
}
对此的任何帮助将不胜感激。
I am looking to update my code to loop my swtich statement if a user inputs anything that doesn't have an option defined in it. I have scoured the numerous pages here that return from various search terms and have come close but so far no luck still.
My code here should get any one that wants to take a stab at it going.
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");
}
Any assistance in this would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您以一种非常奇怪的方式使用 run 变量。由于您在循环结束时将 run 设置为
false
,因此循环将永远不会重复。如果您将其更改为仅设置有效选项run=false
,则输入错误的选项将导致循环再次运行。删除
switch
语句末尾的run=false
,将其添加到System.out.println("OptionX");
之后You are using the run variable in a very odd way. Since you set the run to
false
at the end of the loop, the loop will never repeat. If you change it around so that only the valid options setrun=false
, inputting the wrong option will cause the loop to run one more time.Remove
run=false
at the end of theswitch
statement, add it after theSystem.out.println("OptionX");
“run=false”部分不在正确的位置,无论答案(有效与否)它都会执行。
您应该移动“run=false;”在每个有效的 case 语句中,例如:
顺便说一下:“while (run==true)”是多余的,你可以写“while(run)”
The "run=false" part is not in the right place, it's executed whatever the answer (valid or not).
You should move the "run=false;" inside each valid case statement,like :
By the way : "while (run==true)" is redundant, you can write "while(run)"
您可以将标签添加到循环中。这样,当您获得正确的选项时,您将退出循环。
You can Add a label to your loop. This way when you got the right option you'll exit the loop.
问题是,在默认情况下执行语句后,它将布尔值 run 设置为 false,从而跳出循环。我们需要的是一种跳过这个:-
并直接进入循环条件的方法。一种解决方案是在默认情况下添加“继续”语句:-
The problem is that after executing statement under default, it sets the boolean run to false, and thus, comes out of the loop. What we need is a way to skip this:-
and instead go directly to loop condition. One solution would be to add a 'continue' statement under default:-