java - 从 switch case Java 中调用变量

标签 java while-loop switch-statement

我正在用java编写一个简单的恒温器程序。我在 while 循环中有 switch case。在 switch 语句中,用户选择选项。我想在循环的最后显示该选项。在这里,我想在 switch 语句和 while 循环之后在 GUI 中显示 ttemp、tmode、tfan。显示找不到符号错误。

boolean run=true;

   String[] menuMain ={"Mode","Fan","Temp","PowerOff"};
   String[] menuMode ={"Cool","Heat","Off","Auto"};
   String[] menuFan ={"On","Off","Auto"};
   String[] menuTemp ={"Up","Down","C<>F"};

   String curtemp1= "Current Temperature: "+curTemp;
   JOptionPane.showMessageDialog(null,curtemp1);
   while(run){


      int selMain =JOptionPane.showOptionDialog(null,"Select Menu item","Menu",
      0,3,null,menuMain,menuMain[0]);

     String tmode;
     String tfan;
     double ttemp;

      switch(selMain){
         case 0:  //Mode
            int selMode=JOptionPane.showOptionDialog(null,"Select a Mode",
            "Menu",0,1,null,menuMode,menuMode[3]);

            if (selMode==0)   {tmode="Cool";}
            if (selMode==1)   {tmode="Heat";}
            if (selMode==2)   {tmode="Off";}
            if (selMode==2)   {tmode="Auto";}

            break;
         case 1: //Fan
            int selFan=JOptionPane.showOptionDialog(null,"Select Options",
            "Menu",0,1,null,menuFan,menuFan[2]);
            if (selFan==0)   {tfan="On";}
            if (selFan==1)   {tfan="Off";}
            if (selFan==2)   {tfan="Auto";}

            break;
         case 2:  //Temp
            int selTemp=JOptionPane.showOptionDialog(null,"Select Options",
            "Menu",0,1,null,menuTemp,menuTemp[2]);

            if (selTemp==0)   {curTemp++;}
            if (selTemp==1)   {curTemp--;}
            //if (selTemp==2)   
            ttemp=curTemp;


            break;   
         default: //Off
            JOptionPane.showMessageDialog(null,"Power Off !");
            run=false;

         }  //end switch
         }  //end while loop
         JOptionPane.showMessageDialog(null,ttemp,tmode,tfan);
         }
}

最佳答案

你需要在while循环之前声明这些变量

boolean run=true;

   String[] menuMain ={"Mode","Fan","Temp","PowerOff"};
   String[] menuMode ={"Cool","Heat","Off","Auto"};
   String[] menuFan ={"On","Off","Auto"};
   String[] menuTemp ={"Up","Down","C<>F"};

   String curtemp1= "Current Temperature: "+curTemp;
   JOptionPane.showMessageDialog(null,curtemp1);

   String tmode;
   String tfan;
   double ttemp;
   while(run){


      int selMain =JOptionPane.showOptionDialog(null,"Select Menu item","Menu",
      0,3,null,menuMain,menuMain[0]);


      switch(selMain){
         case 0:  //Mode
            int selMode=JOptionPane.showOptionDialog(null,"Select a Mode",
            "Menu",0,1,null,menuMode,menuMode[3]);

            if (selMode==0)   {tmode="Cool";}
            if (selMode==1)   {tmode="Heat";}
            if (selMode==2)   {tmode="Off";}
            if (selMode==2)   {tmode="Auto";}

            break;
         case 1: //Fan
            int selFan=JOptionPane.showOptionDialog(null,"Select Options",
            "Menu",0,1,null,menuFan,menuFan[2]);
            if (selFan==0)   {tfan="On";}
            if (selFan==1)   {tfan="Off";}
            if (selFan==2)   {tfan="Auto";}

            break;
         case 2:  //Temp
            int selTemp=JOptionPane.showOptionDialog(null,"Select Options",
            "Menu",0,1,null,menuTemp,menuTemp[2]);

            if (selTemp==0)   {curTemp++;}
            if (selTemp==1)   {curTemp--;}
            //if (selTemp==2)   
            ttemp=curTemp;


            break;   
         default: //Off
            JOptionPane.showMessageDialog(null,"Power Off !");
            run=false;

         }  //end switch
      }  //end while loop
      JOptionPane.showMessageDialog(null,ttemp,tmode,tfan);
   }
}

因为此变量仅在您声明它的上下文中可见(在您的示例中是 while 循环。而在此上下文之外,该变量将变得不可见

关于java - 从 switch case Java 中调用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60575613/

相关文章:

java - 根据点分隔符分割字符串

java - 如何使用嵌入式 Jetty 在 Spring Boot 中禁用 org.eclipse.jetty.server.error_page.global 映射到/错误

C-比较两个四位数字数组的函数

Java:有没有一种方法可以在取消部署我的应用程序之前删除所有 ThreadLocal?

Java 程序读取用户输入直到输入 0,同时还会进行一些计算(偶数/奇数、平均值等)

php - 如何改进和加速这个使用嵌套循环的 PHP 脚本?

perl - Perl 不推荐使用的给定/何时的推荐替代品是什么?

java - "How can I request an if-else input after a switch statement?"

java - 通过 eclipse 创建的 BIRT 报告在执行时抛出错误

java - 无法解决android中的错误?