2014-02-17 94 views
0

那麼,這個代碼的實際運行,但結束了錯誤的輸出(特別是在「隨機量」的情況下)JOptionPane的帶開關的情況下

注意:它有一個隨機的結果或輸出

我會隨機生成想知道這段代碼有什麼問題。

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

什麼是錯誤的具體???我沒有觀察到任何錯誤 – 2014-02-17 18:10:18

+0

我的答案是否解決了您的問題? –

+0

問題已解決...謝謝!只是有點困惑 – user3320187

回答

0

你想

switch(select1) 

,而不是

switch(random) 
+0

噢,謝謝你的迴應。這裏是流程...無論你想輸入什麼樣的整數,結果都是隨機的。這就是爲什麼我決定用JOptionPane.showMessageDialog(null,「」** + random **)解決我的問題;即使與案件。 – user3320187