Java 带开关盒的 JOptionPane
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21835975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:59:21 来源:igfitidea点击:
JOptionPane with switch cases
提问by user3320187
Well, this code actually run but ended up with wrong output (specifically in "Randoms" cases)
好吧,这段代码实际上运行但最终输出错误(特别是在“随机”情况下)
Note: it has a random generator for a random result or output
注意:它有一个随机生成器用于随机结果或输出
I would like to know what's wrong with this code.
我想知道这段代码有什么问题。
package gametest1;
import java.util.Random;
import javax.swing.JOptionPane;
public class Gametest1
{
private static int select1;
public static void main(String[] args)
{
int menu1;
do
{
String menu = JOptionPane.showInputDialog("THE BIRTHDAY GAME"+"\n\nMENU"+"\n1.PLAY"+"\n2.EXIT"+"\n\n");
menu1 = Integer.parseInt(menu);
switch(menu1)
{
case 1:
do
{
String select = JOptionPane.showInputDialog("Choose\n"+"\nPRESS '6' TO EXIT"+"\n\n"+"\nENTER YOUR BIRTHDAY FROM 1-5");
int select1 = Integer.parseInt(select);
Random generator = new Random();
if (select1 == 6)
{
JOptionPane.showMessageDialog(null,"BACK TO MAIN MENU!");
break;
}
int random = generator.nextInt(select1);
switch(random)
{
case 1:
{
JOptionPane.showMessageDialog(null,"RANDOMmessage1"+random);
break;
}
case 2:
{
JOptionPane.showMessageDialog(null,"RANDOMmessage2"+random);
break;
}
case 3:
{
JOptionPane.showMessageDialog(null,"RANDOMmessage3"+random);
break;
}
case 4:
{
JOptionPane.showMessageDialog(null,"RANDOMmessage4"+random);
break;
}
case 5:
{
JOptionPane.showMessageDialog(null,"RANDOMmessage5"+random);
break;
}
default:
{
JOptionPane.showMessageDialog(null,"WRONG INPUT");
break;
}
}
}
while (select1 !=5);
break;
case 2:
JOptionPane.showMessageDialog(null,"menu case2: adios!");
System.exit(0);
default:
JOptionPane.showMessageDialog(null,"Program will return");
break;
}
}
while (menu1 !=2);
}
}
回答by Hugo Sousa
You want
你要
switch(select1)
instead of
代替
switch(random)