我想做一个简单的菜单,有 3 个选项:
员工管理器中的“创建新员工”、“显示所有员工”和“退出”(代码如下)但不成功(编译错误)。
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);
}
}