带开关盒的基本 Java 菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19731912/
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
Basic Java Menu with Switch Case
提问by user2943817
The code will compile, but there seems to be an error with my menu. The user will select one of the choices and the program should execute, but When choosing a selection nothing happens. Here is the code:
代码将编译,但我的菜单似乎有错误。用户将选择其中一个选择,程序应该执行,但在选择选择时,没有任何发生。这是代码:
import java.util.Scanner;
class Tutorial{
public static void main(String args[]){
Geek myGeek = new Geek("Geek");
System.out.println("Command Options: ");
System.out.println("a: Geek's Name");
System.out.println("b: Num Questions Asked");
System.out.println("c: All Numbers Are the Same");
System.out.println("d: Sum Between Two Integers");
System.out.println("e: Repeat the String");
System.out.println("f: It is Palindrome");
System.out.println("?: Display");
System.out.println("q: Quit");
Scanner scan = new Scanner(System.in);
String choice = scan.nextLine();
do {
switch (choice){
case "a":
myGeek.getName();
break;
case "b":
myGeek.getnumberofQuestions();
break;
case "c":
System.out.println("Enter the first number");
int input1 = scan.nextInt();
System.out.println("Enter the second number");
int input2 = scan.nextInt();
System.out.println("Enter the third number");
int input3 = scan.nextInt();
myGeek.allTheSame(input1, input2, input3);
break;
case "d":
System.out.println("Enter the first number");
int num1 = scan.nextInt();
System.out.println("Enter the second number");
int num2 = scan.nextInt();
myGeek.sum(num1, num2);
break;
case "e":
System.out.println("Enter a string: ");
String word1 = scan.nextLine();
System.out.println("Enter an integer: ");
int numberOfTimes = scan.nextInt();
System.out.println("Enter the third number");
myGeek.repeat(word1, numberOfTimes);
break;
case "f":
System.out.println("Enter a string: ");
String word2 = scan.nextLine();
myGeek.isPalindrome(word2);
break;
case "?":
System.out.println("Command Options: ");
System.out.println("a: Geek's Name");
System.out.println("b: Num Questions Asked");
System.out.println("c: All Numbers Are the Same");
System.out.println("d: Sum Between Two Integers");
System.out.println("e: Repeat the String");
System.out.println("f: It is Palindrome");
System.out.println("?: Display");
System.out.println("q: Quit");
break;
} }while (choice != "q");
}
}
Here is the what it looks like when run:
这是运行时的样子:
回答by crownedzero
This also depends on which version of the JDK they're using.
这也取决于他们使用的 JDK 版本。
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
回答by Алексей
Few things:
几样东西:
You read input only once - outside of do..while - probably not what you want (otherwise you'd be stuck in infinite loop).
most likely the intent was this: while ((choice = scan.nextLine()) != "q");
As far as why you don't see anything when running, it depends on what myGeek.getName()
does.
As name suggests its a simple getter, if this is a case then it returns the name but it does not print anything on the screen.
您只读取一次输入 - 在 do..while 之外 - 可能不是您想要的(否则您会陷入无限循环)。最有可能的意图是:while ((choice = scan.nextLine()) != "q");
至于为什么您在运行时看不到任何东西,这取决于看到了什么myGeek.getName()
。顾名思义,它是一个简单的 getter,如果是这种情况,则它会返回名称,但不会在屏幕上打印任何内容。
回答by Thangnv
Your loop (do {} while(condition)) will loop infinite when you enter some string different "q" because condition always is true. try with :
当您输入一些不同的字符串“q”时,您的循环 (do {} while(condition)) 将无限循环,因为条件始终为真。尝试:
while (!choice.equals("q")) {
switch (choice) {
case "a":
myGeek.getName();
break;
case "b":
myGeek.getnumberofQuestions();
break;
case "c":
System.out.println("Enter the first number");
int input1 = scan.nextInt();
System.out.println("Enter the second number");
int input2 = scan.nextInt();
System.out.println("Enter the third number");
int input3 = scan.nextInt();
myGeek.allTheSame(input1, input2, input3);
break;
case "d":
System.out.println("Enter the first number");
int num1 = scan.nextInt();
System.out.println("Enter the second number");
int num2 = scan.nextInt();
myGeek.sum(num1, num2);
break;
case "e":
System.out.println("Enter a string: ");
String word1 = scan.nextLine();
System.out.println("Enter an integer: ");
int numberOfTimes = scan.nextInt();
System.out.println("Enter the third number");
myGeek.repeat(word1, numberOfTimes);
break;
case "f":
System.out.println("Enter a string: ");
String word2 = scan.nextLine();
myGeek.isPalindrome(word2);
break;
case "?":
System.out.println("Command Options: ");
System.out.println("a: Geek's Name");
System.out.println("b: Num Questions Asked");
System.out.println("c: All Numbers Are the Same");
System.out.println("d: Sum Between Two Integers");
System.out.println("e: Repeat the String");
System.out.println("f: It is Palindrome");
System.out.println("?: Display");
System.out.println("q: Quit");
break;
}
}
回答by kiruwka
Well, you definitely need to move code which gets input inside the loop :
好吧,您肯定需要移动在循环内获取输入的代码:
String choice = null;
Scanner scan = new Scanner(System.in);
do {
choice = scan.nextLine();
switch (choice) {
case "a":
.........
} // end of switch
} while (!choice.equals("q")); // end of loop
Otherwise, you input once and switch on that input indefinitely (unless it is "q")
否则,您输入一次并无限期地打开该输入(除非它是“q”)
Edit :You also need to change terminating condition to while (!choice.equals("q"));
for it to work.
编辑:您还需要将终止条件更改while (!choice.equals("q"));
为使其工作。
回答by knoight
One issue you have, as mentioned by @rgettman, is that comparing String
s in Java using ==
or !=
will compare the Object reference of the String
not the value; basically are the two String
s the same Object? In this case (and most cases) you want to compare the value.
正如@rgettman 所提到的,您遇到的一个问题是String
在 Java中比较s 使用==
or!=
将比较对象引用而String
不是值;这两个基本上是String
同一个对象吗?在这种情况下(和大多数情况下),您想要比较该值。
Change while (choice != "q");
to while (!choice.equals("q"));
to compare the values.
更改while (choice != "q");
为while (!choice.equals("q"));
以比较值。
A slightly different explanation:
稍微不同的解释:
Right now you are entering a character, say "a", matching your case
for "a" and breaking from the switch/case. However when your program gets to the while
it basically checks whether choice
is"q"
so your program goes back into the do/while
loop.
现在你正在输入一个字符,说“a”,匹配你case
的“a”并打破开关/案例。然而,当你的程序得到的while
是否基本的检查choice
是"q"
让你的程序返回到do/while
循环。
回答by Andrew Evt
i think you want something like this:
我想你想要这样的东西:
....
System.out.println("d: Sum Between Two Integers");
System.out.println("e: Repeat the String");
System.out.println("f: It is Palindrome");
System.out.println("?: Display");
System.out.println("q: Quit");
String choice;
do {
System.out.println("Select something: ");
Scanner scan = new Scanner(System.in);
choice = scan.nextLine();
switch (choice){
case "a":
myGeek.getName();
break;
case "b":
myGeek.getnumberofQuestions();
break;
case "c":
System.out.println("Enter the first number");
int input1 = scan.nextInt();
System.out.println("Enter the second number");
....
if not --->>>> for what you need do -> while??
如果不是 --->>>> 为您需要做的 -> 而?
and check, if your java is 7 or heighter, and check your getMethods() -> if thay return anything
并检查您的 java 是否为 7 或更高,并检查您的 getMethods() -> 是否返回任何内容