如何在switch-case语句中获取和设置值

时间:2012-06-13 05:04:57

标签: java menu switch-statement

我想制作一个包含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);
    }
}

3 个答案:

答案 0 :(得分:2)

它们是本地阻止,将它们从开关块中声明

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

switch(){...}

这很好地回答了你的问题,但我想补充一些细节

如果你不把括号放在像

这样的案例块中
 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实例在其他案例块中也可见

答案 1 :(得分: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 () {
    ...  

}

答案 2 :(得分:0)

在交换机外部更广泛的范围内定义对象m,s和l。另外,使用null值初始化对象并在使用前验证它们。

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
            }
    }
}