在while循环终止后显示的while循环中的JOptionPane

时间:2014-10-16 14:02:38

标签: java while-loop joptionpane

所以程序成功运行,除了当满足while循环的终止条件时,它会在显示消息对话框之前发布最后一个JOptionPane输入对话框以结束程序,输入对话框将完全没有任何结果。

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.");
}
}  

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

滚动后立即重新计算diceTotal,这样你的while循环就会更快退出。