【问题标题】:How to get and set value in the switch-case statement如何在 switch-case 语句中获取和设置值
【发布时间】:2012-06-13 05:04:57
【问题描述】:

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

【问题讨论】:

    标签: java menu switch-statement


    【解决方案1】:

    它们是局部阻塞的,将它们声明在开关块之外

    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 实例在其他 case 块中也是可见的

    【讨论】:

    • 我不明白为什么当 OP 想要在选项 1 中创建对象时创建对象,而不是之前。
    • 谢谢,吉加尔!但是当我切换到案例 2 时,所有值都为空。在我添加“简单菜单”之前,它们都可以工作。
    • 没听明白,能否请您更新问题并通知我们
    • 你最好1)将“你正在做的事情”分组到单独的方法中,2)制作“你正在使用的数据”“会员数据”,最重要的是,制作它是一个真正的“类”(而不是依赖于“静态主()”。即使是这样的简单案例。恕我直言......
    • PS:对于任何严肃的 Java 开发,我强烈推荐像 NetBeans 或 Eclipse 这样的 IDE,而不是像 BlueJ 这样的教学工具。再次:恕我直言...
    【解决方案2】:

    问:有没有办法在'case 1'中获取对象的值并在'case 2'中使用它们?

    答:不。case 块的全部意义在于“非此即彼”。

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

    示例:

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

    }

    【讨论】:

    • 谢谢!它似乎有效,但我有一个错误:主块中的“不兼容的类型 - 发现无效但预期测试”。 ——
    【解决方案3】:

    在开关之外更广泛的范围内定义您的对象 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
                }
        }
    }
    

    【讨论】:

    • 谢谢路易吉!但我想在 op1 中设置值,然后获取它们并在 op2 中显示。有可能吗?
    • @K.Dam 是的,我已经更新了我的帖子,我忘了你是在循环内声明对象,它们应该在更广泛的范围内,比如在 do 之外或作为你的类的属性。
    • 我已经尝试过您的代码,但 op2 中似乎没有任何内容。 :( 可能我的其他班级有问题...但感谢您的帮助!我会记住您的提示!祝您有美好的一天!
    • @K.Dam 欢迎来到 StackOverflow。当您提出问题并获得好的答案时,您应该mark the post as an answer 以便其他有类似问题的人可以轻松找到答案:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 2014-08-05
    • 2019-04-07
    相关资源
    最近更新 更多