如果声明不能正常工作

时间:2013-11-13 03:57:37

标签: java if-statement

正如你所知,我是Stack Over Flow的新手。我编程时,我几乎总是在这里阅读帖子。这似乎是一个非常好的地方,所以我决定加入。谢谢你让我发垃圾邮件。

我的代码几分钟前正在运行。我很确定我在事故中做的事情很愚蠢。当我输入一个数字时,每个菜单选项除了7个作品。当我输入7弹出一个方框,说无效选项。使用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();
    }
}

}

由于我的副本和粘贴到这里,格式化有点混乱。

3 个答案:

答案 0 :(得分:1)

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

首先,我会做一个

whatToDo = whatToDo.trim();

在做一些比较之前。

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

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

答案 1 :(得分: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();
        }
    }

答案 2 :(得分:0)

您是否搞砸了Alchemy.startAlchemy代码?我猜问题就在那里