單擊取消或退出按鈕后如何添加功能?我像這樣嘗試過,但對我不起作用。
它向我顯示了一個錯誤,選擇為空,但不可能是因為它是 int?有沒有其他可能來解決它???
public void start() {
int choice = Integer.parseInt(JOptionPane.showInputDialog(null, "Choice: \n 1 - Play a game \n 2 - show all games \n 3 - Find the best score \n 4 - Find a player \n 5 - End"));
//if (choice == JOptionPane.OK_OPTION) {
switch (choice) {
case 1:
this.play();
break;
case 2:
this.allGames();
break;
case 3:
this.getBestScore();
break;
case 4:
this.getPlayer();
break;
case 5:
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "Wrong input.");
break;
}
// } else if (choice == JOptionPane.CANCEL_OPTION) {
// System.exit(0);
// } else if (choice == JOptionPane.CLOSED_OPTION) {
// System.exit(0);
// }
}
uj5u.com熱心網友回復:
當您按下“退出”或“取消”按鈕時,您將“選擇”分配給空值,但這不起作用,因為選擇是一個整數。您可以“選擇”一個字串,然后在 switch 陳述句之前確保選擇!=null。這就是它的樣子
public static void start() {
String choice = JOptionPane.showInputDialog(null, "Choice: \n 1 - Play a game \n 2 - show all games \n 3 - Find the best score \n 4 - Find a player \n 5 - End");
if(choice!=null) {
switch (choice) {
case "1":
play();
break;
case "2":
allGames();
break;
case "3":
getBestScore();
break;
case "4":
getPlayer();
break;
case "5":
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "Wrong input.");
break;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/394214.html
