-1

正如你所知道的,我是这个 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();
    }
}

}

由于我的复制和粘贴到这里,格式有点混乱。

4

3 回答 3

1

哇,这是一些非常糟糕的代码。

一方面,我会做一个

whatToDo = whatToDo.trim();

在做一些比较之前。

您是否尝试过在调试中运行或放置 System.out.println 以查看 Alchemy.startAlchemy() 方法是否有问题?

if/else 逻辑看起来没问题。

于 2013-11-13T04:03:09.773 回答
1

我相信您在执行选项 7 的代码时遇到了异常:

 else if (whatToDo.equalsIgnoreCase("Alchemy") || whatToDo.equalsIgnoreCase("7")) {
        Alchemy.startAlchemy();
    }

根据 catch 块,如果有异常,它会在执行以下语句时说“无效选项”

JOptionPane.showMessageDialog(null, "Invalid Option",
                "Text Based Game", JOptionPane.PLAIN_MESSAGE);

所以问题出在Alchemy.startAlchemy()而不是else if。如果您像这样封装代码,它将确认问题出在哪里:

 else if (whatToDo.equalsIgnoreCase("Alchemy") || whatToDo.equalsIgnoreCase("7")) {
        try {
        Alchemy.startAlchemy();
        } catch(Exception e) {
           System.out.println("The problem is not with else if but the exception");
           e.printStackTrace();
        }
    }
于 2013-11-13T04:04:25.290 回答
0

你搞砸了 Alchemy.startAlchemy 代码吗?我想问题出在那儿

于 2013-11-13T04:03:37.873 回答