'JOptionPane with switch cases
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);
}
}
Solution 1:[1]
You want
switch(select1)
instead of
switch(random)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Hugo Sousa |