2014-10-16 173 views
0

所以程序成功運行,除了當滿足while循環的終止條件時,它會在顯示消息對話框以結束程序之前發佈最後的JOptionPane輸入對話框,輸入對話框完全沒有任何作用。JOptionPane在while循環內顯示while循環後終止

import javax.swing.JOptionPane; 
import java.util.Arrays; 

public class DiceGame2{ 
public static void main(String[] args){ 
    Dice[] dDice = new Dice[5]; 
    String[] sDice = new String[5]; 
    String finalSDice, sRoll; 
    int diceTotal=1, oldTotal=0, iRoll, numberOfRolls=0; 

    for(int i=0; i<5; i++){ 
     dDice[i]= new Dice(); 
    } 

    for(int i=0; i<5; i++){ 
     dDice[i].roll(); 
    } 
    while(diceTotal>=oldTotal){ 
     for(int i=0; i<5; i++){ 
      sDice[i]=Integer.toString(dDice[i].value()); 
     } 
     finalSDice=Arrays.toString(sDice);   
     oldTotal=diceTotal; 
     diceTotal=0; 
     for(int i=0; i<5; i++){ 
      diceTotal+=dDice[i].value(); 
     } 
     sRoll=JOptionPane.showInputDialog(null,"Your 5 dice contain the numbers:\n"+finalSDice+"\nThese dice have a total score of "+diceTotal+".\nEnter which dice you would like to reroll to attempt to increase your score:"); 
     iRoll=Integer.parseInt(sRoll); 
     dDice[iRoll-1].roll(); 
     numberOfRolls++; 
    } 
    finalSDice=Arrays.toString(sDice); 
    JOptionPane.showMessageDialog(null, "Your 5 dice contain the numbers:\n"+finalSDice+"\nThese dice have a total score of "+diceTotal+".\nYou've failed to increase your total score.\n You made "+numberOfRolls+" successful rolls."); 
} 
} 

我該如何解決這個問題?

+0

不要在循環中使用JOptionPane :)。 您正在創建幀循環它不好,因爲你不能 – 2014-10-16 14:04:21

+0

我看不到你的代碼在哪裏做這件事,你能告訴我們輸入和輸出嗎? – DreadHeadedDeveloper 2014-10-16 14:06:11

+0

@DreadHeadedDeveloper只有兩個JOptionPane對話框,程序是一個非常簡單的骰子計數器。 – Scy 2014-10-16 14:08:37

回答

0

在滾動後立即重新計算diceTotal,所以while循環會更快地退出。

+0

是啊!就是這樣 – Scy 2014-10-16 18:06:47