java 如何在 switch-case 语句中获取和设置值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11008478/
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
How to get and set value in the switch-case statement
提问by K.Dam
I want to make a simple menu with 3 choices:
我想制作一个有 3 个选择的简单菜单:
'Create new employee', 'Display all employees' and 'Quit' in a Employee Manager(code below) but it was not successful(compiling error).
“创建新员工”、“显示所有员工”和“退出”在员工管理器中(下面的代码)但没有成功(编译错误)。
BlueJ editor cannot realize the object 'm', 's' and 'l' in the 'case 2' statement. Is there anyway to get the value of the object in the 'case 1' and use them in the 'case 2'? Thanks a lot!
BlueJ 编辑器无法实现'case 2' 语句中的对象'm'、's' 和'l'。无论如何要在“案例1”中获取对象的值并在“案例2”中使用它们?非常感谢!
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
int ch;
do{
System.out.println("EMPLOYEE MANAGER\n");
System.out.println("1. Create new employees\n");
System.out.println("2. Display all employees\n");
System.out.println("3. Quit\n");
System.out.print("Your choice: ");
Scanner input = new Scanner(System.in);
ch = input.nextInt();
switch(ch){
case 1: System.out.println("== CREATE NEW EMPLOYEES ==");
System.out.println();
Manager m = new Manager();
Scientist s = new Scientist();
Labourer l = new Labourer();
m.newManager();
s.newScientist();
l.newLabourer();
System.out.println();
break;
case 2: System.out.println("== PREVIEW EMPLOYEES ==");
System.out.println();
m.display();
s.display();
l.display();
System.out.println();
System.out.println();
break;
case 3: System.exit(0);
default: System.out.println("Invalid choice!");
}
} while(ch >= 1 && ch <=4);
}
}
回答by Jigar Joshi
They are local to block, declare them out of switch block
它们是本地块,在 switch 块之外声明它们
Manager m = new Manager();
Scientist s = new Scientist();
Labourer l = new Labourer();
switch(){...}
This answers your question well, but I would like to add few more details
这很好地回答了您的问题,但我想添加更多细节
if you don't put brackets with case block like
如果你不把括号和 case 块放在一起
switch(i){
case 1:
String str="abc";
System.out.println(str);
case 2:
// it will give you compile time error
//duplcate local variable str
String str="abc";
}
then this str
instance is visible in other case blocks as well
那么这个str
实例在其他案例块中也是可见的
回答by paulsm4
Q: Is there anyway to get the value of the object in the 'case 1' and use them in the 'case 2'?
问:无论如何,是否可以在“案例 1”中获取对象的值并在“案例 2”中使用它们?
A: No. The whole point of a case block is "either-or".
答:不可以。案例块的重点是“非此即彼”。
If you want to do "something" based on "something else", then you'll need two separate control structures.
如果您想根据“其他事情”做“某事”,那么您将需要两个单独的控制结构。
EXAMPLE:
例子:
import java.util.Scanner;
public class Test
{
Manager m = null;
Scientist s = null;
Labourer l = null;
public static void main(String[] args) {
Test test = new Test().doIt ();
}
private void doIt () {
int ch;
do{
System.out.println("EMPLOYEE MANAGER\n");
System.out.println("1. Create new employees\n");
System.out.println("2. Display all employees\n");
System.out.println("3. Quit\n");
System.out.print("Your choice: ");
Scanner input = new Scanner(System.in);
ch = input.nextInt();
switch(ch) {
case 1:
System.out.println("== CREATE NEW EMPLOYEES ==");
getEmployees ();
break;
case 2:
System.out.println("== PREVIEW EMPLOYEES ==");
previewEmployees ();
break;
case 3:
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
}
} while(ch >= 1 && ch <=4);
}
private void getEmployees () {
System.out.println();
m = new Manager();
s = new Scientist();
labourer l = new Labourer();
m.newManager();
s.newScientist();
l.newLabourer();
System.out.println();
}
private void previewEmployees () {
...
}
}
回答by Luiggi Mendoza
Define your objects m, s and l in a broader scope outside the switch. Also, initialize your objects with null value and validate them before using.
在 switch 之外的更广范围内定义对象 m、s 和 l。此外,使用空值初始化您的对象并在使用前验证它们。
Manager m = null;
Scientist s = null;
Labourer l = null;
do{
//your code here....
switch(ch) {
case 1:
m = new Manager();
//the rest of your code here...
break;
case 2:
if (m != null) {
m.display(); //and so on
}
}
}