0

我想做一个简单的菜单,有 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);
    }
}
4

3 回答 3

2

它们是本地阻塞的,将它们声明在 switch 块之外

Manager   m = new Manager();
Scientist s = new Scientist();
Labourer  l = new Labourer();

switch(){...}

这很好地回答了您的问题,但我想添加更多细节

如果您不将括号与 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";

    }

那么这个str实例在其他案例块中也是可见的

于 2012-06-13T05:06:21.660 回答
1

问:有没有办法在“案例1”中获取对象的值并在“案例2”中使用它们?

答:不。案例块的重点是“非此即彼”。

如果您想基于“其他”做“某事”,那么您将需要两个独立的控制结构。

例子:

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 () {
    ...  

}

于 2012-06-13T05:09:06.337 回答
0

在交换机之外更广泛的范围内定义您的对象 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
            }
    }
}
于 2012-06-13T05:06:43.577 回答