嗯,这段代码实际上运行但输出错误(特别是在“Randoms”情况下)
注意:它有随机生成器用于随机结果或输出
我想知道这段代码有什么问题。
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);
}
}
答案 0 :(得分:0)
你想要
switch(select1)
而不是
switch(random)