带开关盒的JOptionPane

问题描述 投票:0回答:1

好吧,这段代码实际运行但输出错误(特别是在“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);
    }

}
java swing random switch-statement do-while
1个回答
0
投票

你要

switch(select1)

代替

switch(random)
© www.soinside.com 2019 - 2024. All rights reserved.