gpt4 book ai didi

java - 当我单击“否”或“取消”选项时,为什么此 JOptionPane 程序没有关闭?

转载 作者:行者123 更新时间:2023-12-01 11:41:51 26 4
gpt4 key购买 nike

在该程序的第一个对话框中,单击"is"、“否”或“取消”都会执行相同的操作。如果用户单击“否”或“取消”,如何关闭程序?我可以使用一些异常处理吗?或者也许是另一个循环?

package dice;
import java.awt.Component;
import javax.swing.JOptionPane;

public class DialogBox
{
public static void main(String[] args) throws IllegalStateException
{
int again = 0, money, guess, bet = 0;
String message, message2, message3, guessStr, betStr, resultStr;
Account account = new Account(100);

IllegalStateException problem = new IllegalStateException("You're out of cash!");

do
{
money = account.getCashValue();
message = "You have $" + money + ". Would you like to bet?";
JOptionPane.showConfirmDialog(null, message);
betStr = JOptionPane.showInputDialog("How much would you like to bet?($): ");
bet = Integer.parseInt(betStr);

guessStr = JOptionPane.showInputDialog("Guess the sum of two die, then press OK to roll the die.");
guess = Integer.parseInt(guessStr);

Dice dice1 = new Dice(6);
Dice dice2 = new Dice(6);

dice1.setValue();
dice2.setValue();

int diceSum = (dice1.getValue() + dice2.getValue());
boolean win = Account.winBet(guess, diceSum);

if (win == true)
resultStr = "You win! \n";
else
resultStr = "You Lose! \n";

message2 = resultStr + "first die: " + dice1.getValue() + "\nsecond die: " + dice2.getValue() + "\nsum of die: " + diceSum;
JOptionPane.showMessageDialog(null, message2);

account.newBalance(win, bet);
money = account.getCashValue();

if (money <= 0)
throw problem;


}
while (again == JOptionPane.showConfirmDialog(null, message));
}

}

最佳答案

您没有将确认对话框的结果分配给任何内容。

message3 = "You now have $" + money + ". Would you like to bet again?";
// vvvv
again = JOptionPane.showConfirmDialog(null, message3);

https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

关于java - 当我单击“否”或“取消”选项时,为什么此 JOptionPane 程序没有关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29463784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com