正如你所知道的,我是这个 Stack Over Flow 的新手。不过,当我编程时,我几乎总是在这里阅读帖子。这似乎是一个相当不错的地方,所以我决定加入。谢谢你让我向你发送垃圾邮件。
我的代码几分钟前还在工作。我很确定这是我偶然做的愚蠢的事情。当我输入一个数字时,除了 7 之外的每个菜单选项都有效。当我在 7 中输入时,会弹出一个框,说 Invalid option。使用 if 语句,我推断 whatToDo 实际上可以等于 7,但它仍然会弹出框。
package TB_Game;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class TB_Game {
public static void main(String[] args) {
menu();
}
public static void menu() {
String whatToDo;
try {
whatToDo = JOptionPane.showInputDialog("Welcome to the TextBased Game! " + "\n"
+ "\n" + "Which game do you wish to play?" + "\n"
+ "1 - Gangup - Work in Progress" + "\n"
+ "2 - Black Jack - Not Been Finished" + "\n"
+ "3 - Encryption - Non GUI" + "\n"
+ "4 - Decryption - Non GUI" + "\n"
+ "5 - Encryption GUI" + "\n"
+ "6 - Decryption GUI" + "\n"
+ "7 - Alchemy - Work In Progress" + "\n"
+ "8 - Exit");
if (whatToDo.equalsIgnoreCase("Gangup") || whatToDo.equalsIgnoreCase("1")) {
gangup.startGangup();
}
else if (whatToDo.equalsIgnoreCase("Black Jack") || whatToDo.equalsIgnoreCase("2")) {
JOptionPane.showMessageDialog(null, "Black Jack has not been finished",
"Text Based Game", JOptionPane.PLAIN_MESSAGE);
menu();
}
else if (whatToDo.equalsIgnoreCase("Encryption") || whatToDo.equalsIgnoreCase("3")) {
cryption.encrypt();
}
else if (whatToDo.equalsIgnoreCase("Decryption") || whatToDo.equalsIgnoreCase("4")) {
cryption.decrypt();
}
else if (whatToDo.equalsIgnoreCase("Encryption GUI") || whatToDo.equalsIgnoreCase("5")) {
cryptionGUI.encrypt();
}
else if (whatToDo.equalsIgnoreCase("Decryption GUI") || whatToDo.equalsIgnoreCase("6")) {
cryptionGUI.decrypt();
}
else if (whatToDo.equalsIgnoreCase("Alchemy") || whatToDo.equalsIgnoreCase("7")) {
Alchemy.startAlchemy();
}
else if (whatToDo.equalsIgnoreCase("Exit") || whatToDo.equalsIgnoreCase("8")) {
exit();
} else {
JOptionPane.showMessageDialog(null, "Invalid Option",
"Text Based Game", JOptionPane.PLAIN_MESSAGE);
menu();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Invalid Option",
"Text Based Game", JOptionPane.PLAIN_MESSAGE);
menu();
}
}
public static void exit() {
int quit;
quit = JOptionPane.showConfirmDialog(null,
"Do you wish to close TB Game?");
if (quit == 0) {
System.exit(0);
} else {
menu();
}
}
}
由于我的复制和粘贴到这里,格式有点混乱。