gpt4 book ai didi

java - 从 switch case Java 中调用变量

转载 作者:行者123 更新时间:2023-12-01 18:04:52 25 4
gpt4 key购买 nike

我正在用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com