Java 我想要一个菜单在无效选项后重复?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24623023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I want a menu to repeat after invalid option?
提问by user3769297
Ok I know this must be simpler than I am making it out to be, but I can't seem to figure it out. I have menu. the user selects a number which calls a certain method.
好吧,我知道这一定比我想象的要简单,但我似乎无法弄清楚。我有菜单。用户选择一个调用特定方法的号码。
I want the menu to repeat if they do not select a valid option. and then I want to give them the option to select another option or end the program.
如果他们没有选择有效选项,我希望菜单重复。然后我想给他们选择另一个选项或结束程序的选项。
Can anyone help me out?
谁能帮我吗?
import java.util.Scanner;
public class TestStringManip {
//=================MAIN===============
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = in.nextLine();
StringManipulation newString = new StringManipulation(input);
String menu = "\n Please select an option to perform"
+ "\n1 (1) Get all uppercase letters."
+ "\n2 (2) Get every second letter."
+ "\n3 (3) Replace vowels with _ "
+ "\n4 (4) Get a count of all vowels."
+ "\n5 (5) Get position of all vowels.";
System.out.println(menu);
int option = in.nextInt();
do
{
if (option == 1)
{
newString.getUpperCase(input);
}
else if (option == 2)
{
newString.getEverySecond(input);
}
else
{
System.out.println("Error must select 1-5");
System.out.println(menu);
option = in.nextInt();
}
}while (option < 1 || option > 5);
}
}
I have updated my code to try some of the suggested answers. It works for the first two options but that is it. I am also trying to get it to repeat after each option is chosen unless 0 is chosen which should end the program. Here is my updated code.
我已经更新了我的代码以尝试一些建议的答案。它适用于前两个选项,但仅此而已。我也试图让它在选择每个选项后重复,除非选择 0 应该结束程序。这是我更新的代码。
import java.util.Scanner;
public class TestStringManip {
//=================MAIN===============
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = in.nextLine();
StringManipulation newString = new StringManipulation(input);
String menu = "\n Please select an option to perform"
+ "\n1 (1) Get all uppercase letters."
+ "\n2 (2) Get every second letter."
+ "\n3 (3) Replace vowels with _ "
+ "\n4 (4) Get a count of all vowels."
+ "\n5 (5) Get position of all vowels."
+ "\n0 (0) Exit Program";
System.out.println(menu);
int option = in.nextInt();
int i = 0;
do
{
if (option == 1)
{
newString.getUpperCase(input);
i = 1;
}
else if (option == 2)
{
newString.getEverySecond(input);
i = 1;
}
else if (option == 3)
{
newString.replaceVowels(input);
i = 1;
}
else if (option == 4)
{
newString.getNumberOfVowels(input);
i = 1;
}
else if (option == 5)
{
newString.getVowelPosition(input);
i = 1;
}
else if (option == 0)
{
break;
}
else
{
System.out.println("Error must select 0-5");
System.out.println("Enter your option: ");
option = in.nextInt();
}
}while (i == 0);
}
}
采纳答案by Frakcool
Not sure if this is what you want to do, repeat the whole menu unless user press 0
, then exit.
不确定这是否是您想要做的,除非用户按0
,否则重复整个菜单,然后退出。
I made it with switch
instead of if-else
. But you can change that piece of code. Try it and tell me if this is what you were looking for.
我用switch
而不是if-else
. 但是您可以更改那段代码。试试吧,告诉我这是否是你要找的。
This one validates the input BEFORE processing it on switch
这个在开关上处理输入之前验证输入
import java.util.Scanner;
class MyTest {
public static void main(String args[]) {
int option;
Scanner in = new Scanner(System.in);
do {
do {
String menu = "\n Please select an option to perform"
+ "\n1 (1) Get all uppercase letters."
+ "\n2 (2) Get every second letter."
+ "\n3 (3) Replace vowels with _ "
+ "\n4 (4) Get a count of all vowels."
+ "\n5 (5) Get position of all vowels."
+ "\n0 (0) Exit Program";
System.out.println(menu);
option = in.nextInt();
} while(option < 0 || option > 5); // This will make the menu repeat if option is higher than 6 or lowen than 0.
switch(option) {
case 1:
System.out.println("Option 1");
break;
case 2:
System.out.println("Option 2");
break;
case 3:
System.out.println("Option 3");
break;
case 4:
System.out.println("Option 4");
break;
case 5:
System.out.println("Option 5");
break;
default:
System.out.println("Hasta la vista Baby");
break; //I always use this break, even when not needed.
}
} while (option != 0);
}
}
EDIT
编辑
As GPRathourpointed out, you can do it with a simple do-while
loop, so it would look like this:
正如GPRathour指出的,你可以用一个简单的do-while
循环来完成,所以它看起来像这样:
This one validates the input AFTER processing it on switch
这个在开关上处理输入后验证输入
import java.util.Scanner;
class MyTest {
public static void main(String args[]) {
int option;
Scanner in = new Scanner(System.in);
do {
String menu = "\n Please select an option to perform"
+ "\n1 (1) Get all uppercase letters."
+ "\n2 (2) Get every second letter."
+ "\n3 (3) Replace vowels with _ "
+ "\n4 (4) Get a count of all vowels."
+ "\n5 (5) Get position of all vowels."
+ "\n0 (0) Exit Program";
System.out.println(menu);
option = in.nextInt();
switch(option) {
case 0:
System.out.println("Hasta la vista Baby");
break;
case 1:
System.out.println("Option 1");
break;
case 2:
System.out.println("Option 2");
break;
case 3:
System.out.println("Option 3");
break;
case 4:
System.out.println("Option 4");
break;
case 5:
System.out.println("Option 5");
break;
default:
System.out.println("Wrong option");
break; //I always use this break, even when not needed.
}
} while (option != 0);
}
}
回答by Rook
Try a switch statement. Replace your if statement with this block of code.
尝试使用 switch 语句。用这段代码替换你的 if 语句。
do{
switch(option){
case 0:
//Exit loop
break;
case 1:
newString.getUpperCase(input);
break;
case 2:
newString.getEverySecond(input);
break;
case 3:
// Method number 3;
. . .
default:
System.out.println("Please enter a number between 1 and 5");
break;
}
} while(option !=0)
EDITIf you want to know more about switch statements, check out this java tutorial
编辑如果您想了解有关 switch 语句的更多信息,请查看此java 教程
Re-edit:And yeah, you'll have to check for int input rather than a string like Ricky stated
重新编辑:是的,你必须检查 int 输入而不是像 Ricky 所说的字符串
回答by Kick Buttowski
code:
代码:
public class TestStringManip {
public static void main(String[] args) {
String menu = "\n Please select an option to perform"
+ "\n1 (1) for option one enter 1."
+ "\n2 (2) for option two enter 2."
+ "\n0 (0) Exit Program";
Scanner in = new Scanner(System.in);
int option = ReadAndMenu(menu,in);
int i = 0;
do {
if (option == 1) {
System.out.println("option one");
option = ReadAndMenu(menu,in);
} else if (option == 2) {
System.out.println("option two");
option = ReadAndMenu(menu,in);
} else if (option == 0){
System.out.println("exit");
i= 1;
} else {
System.out.println("Please enter a number between 1 and 2");
option = ReadAndMenu(menu,in);
}
} while (i == 0);
}
private static int ReadAndMenu(String menu, Scanner in){
System.out.println(menu);
System.out.print("Enter your option: ");
return in.nextInt();
}
}
}
output:
输出:
Please select an option to perform
1 (1) for option one enter 1.
2 (2) for option two enter 2.
0 (0) Exit Program
Enter your option: 1
option one
Please select an option to perform
1 (1) for option one enter 1.
2 (2) for option two enter 2.
0 (0) Exit Program
Enter your option: 2
option two
Please select an option to perform
1 (1) for option one enter 1.
2 (2) for option two enter 2.
0 (0) Exit Program
Enter your option: 5
Please enter a number between 1 and 2
Please select an option to perform
1 (1) for option one enter 1.
2 (2) for option two enter 2.
0 (0) Exit Program
Enter your option: 0
回答by Ricky Mutschlechner
I would add a check to Scanner.hasNextInt() to make sure that the user is actually typing in an int. Because right now, if someone were to type "Hello", it would break your program. I fixed the code, and hopefully the loop.
我会向 Scanner.hasNextInt() 添加一个检查,以确保用户实际上输入的是 int。因为现在,如果有人输入“Hello”,它会破坏你的程序。我修复了代码,希望是循环。
Here's an example, using a modified version of your code: (sorry for the wonky indentation - yours is weird, and I haven't fixed it yet. Will edit with a fix)
这是一个示例,使用您的代码的修改版本:(抱歉缩进不稳定 - 您的缩进很奇怪,我还没有修复它。将编辑修复)
import java.util.Scanner;
public class TestStringManip {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = in.nextLine();
StringManipulation newString = new StringManipulation(input);
String menu = "\n Please select an option to perform"
+ "\n1 (1) Get all uppercase letters."
+ "\n2 (2) Get every second letter."
+ "\n3 (3) Replace vowels with _ "
+ "\n4 (4) Get a count of all vowels."
+ "\n5 (5) Get position of all vowels.";
System.out.println(menu);
int option = getIntInput(in);
do{
if (option == 1)
{
newString.getUpperCase(input);
}
else if (option == 2)
{
newString.getEverySecond(input);
}
else if (option == 0){
System.out.println("Goodbye!");
System.exit(0);
}
else
{
option = getIntInput(in);
}
}while(option != 0);
}
//this function will keep prompting until the user inputs a valid option
int getIntInput(Scanner in){
while(!in.hasNextInt()){
System.out.println("Please enter a valid option (# between 1 and 5)");
}
return in.nextInt();
}
}
回答by gprathour
The way you want your logic to work I believe there should be an option to exit too. So how about trying something like the following,
你希望你的逻辑工作的方式我相信也应该有一个退出的选项。那么尝试以下类似的方法如何,
do{
String menu = "\n Please select an option to perform"
+ "\n1 (1) Get all uppercase letters."
+ "\n2 (2) Get every second letter."
+ "\n3 (3) Replace vowels with _ "
+ "\n4 (4) Get a count of all vowels."
+ "\n5 (5) Get position of all vowels."
+ "\n0 (0) To Exit.";
System.out.println(menu);
int option = in.nextInt();
if (option == 1){
newString.getUpperCase(input);
}
else if (option == 2){
newString.getEverySecond(input);
}
...
...
else if(option == 0){
break;
}else{
System.out.println("\n\nWrong Choice\n\n");
}
}while(option != 0);
EDIT
编辑
As Kick Buttowski was having so many doubts. I created two simple examples, just run them on your own and see the output, as I don't have enough time to upload screenshots of output of both of them.
由于 Kick Buttowski 有很多疑问。我创建了两个简单的示例,只需自己运行它们并查看输出,因为我没有足够的时间上传它们的输出屏幕截图。
MyCodeit will work fine
MyCode它将正常工作
import java.util.Scanner;
class MyTest{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int option = 0;
do{
String menu = "\n Please select an option to perform"
+ "\n1 (1) Get all uppercase letters."
+ "\n2 (2) Get every second letter."
+ "\n3 (3) Replace vowels with _ "
+ "\n4 (4) Get a count of all vowels."
+ "\n5 (5) Get position of all vowels."
+ "\n0 (0) To Exit.";
System.out.println(menu);
option = in.nextInt();
if (option == 1){
System.out.println("1");
}
else if (option == 2){
System.out.println("2");
}
else if(option == 0){
System.out.println("0");
break;
}else{
System.out.println("\n\nWrong Choice\n\n");
}
}while(option != 0);
}
}
To clarify the difference here is the Code proposed by Other userswhich will result into an infinite loop
澄清这里的区别是其他用户提出的代码,这将导致无限循环
import java.util.Scanner;
class MyTest2{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int option = 1;
do{
if (option == 1){
System.out.println("1");
}
else if (option == 2){
System.out.println("2");
}
else if(option == 0){
System.out.println("0");
break;
}else{
System.out.println("\n\nWrong Choice\n\n");
String menu = "\n Please select an option to perform"
+ "\n1 (1) Get all uppercase letters."
+ "\n2 (2) Get every second letter."
+ "\n3 (3) Replace vowels with _ "
+ "\n4 (4) Get a count of all vowels."
+ "\n5 (5) Get position of all vowels."
+ "\n0 (0) To Exit.";
System.out.println(menu);
option = in.nextInt();
}
}while(option != 0);
}
}