java - 用户输入和 Switch 语句的奇怪错误

标签 java switch-statement

public class VoteTUIView {

    VoteMachine voteMachine;

    public VoteTUIView(VoteMachine voteMachine) {
        this.voteMachine = voteMachine;
        this.start();

    }

    public static String errorMissingVariable = "This command needs an variable";
    public static String errorPartyNoneExistant = "This party does not exist";
    public static String errorAddImpossible = "You can't add something to that";
    public static String help = "Seriously? You need help? You look like a smart boy/girl, you can figure this out yourself. I believe in you! ";


    public void start(){
        System.out.println("Please insert a command: (VOTE [party], ADD PARTY [party], VOTES, PARTIES, EXIT, and HELP) ");
        Scanner inputScanner = new Scanner(System.in);
        while(inputScanner.hasNextLine()){
            String inputCommand = inputScanner.next();
            switch (inputCommand){
                case "VOTE": {
                    if(inputScanner.hasNext()){
                        String inputCommand2 = inputScanner.next();
                        if(voteMachine.getParties().getParties().contains(inputCommand2)){
                            voteMachine.vote(inputCommand2);
                        }
                        else{
                            System.out.println(errorPartyNoneExistant);
                        }
                    }
                    else{
                        System.out.println(errorMissingVariable);
                    }
                }
                case "ADD": {
                    if(inputScanner.next().equals("PARTY")) {
                        if(inputScanner.hasNext()) {
                            voteMachine.addParty(inputScanner.next());
                        }
                        else{
                            System.out.println(errorMissingVariable);
                        }
                    }
                    else {
                        showError(errorAddImpossible);
                    }
                }
                case "VOTES": {
                    showVotes(voteMachine.getVotes().getVoteList());
                }
                case "PARTIES": {
                    showParties(voteMachine.getParties().getParties());
                }
                case "EXIT": {
                    break;
                }
                case "HELP": {
                    System.out.println(help);
                }
                default: {
                }
            }

            System.out.println("\nPlease insert a command: (VOTE [party], ADD PARTY [party], VOTES, PARTIES, EXIT, and HELP) ");
        }
    }

    public void showVotes(Map<String, Integer> voteList) {
        int i = 1;
        for(String partyName : voteList.keySet()){
            System.out.println(i+": "+partyName+" has obtained "+voteList.get(partyName)+" votes.");
            i++;
        }
    }

    private void showParties(List<String> partyList) {
        int i = 1;
        for(String partyName : partyList){
            System.out.println(i+": "+partyName);
            i++;
        }
    }

    private void showError(String error) {
        System.out.println("Error: " + error);
    }        
}

我正在与一种奇怪的 bug 作斗争。程序读取用户输入并确定对开关执行哪个操作,但通常,开关中的多个情况在明显不应该的情况下被触发。这让我抓狂。有谁知道为什么这样做?

Please insert a command: (VOTE [party], ADD PARTY [party], VOTES, PARTIES, EXIT, and HELP)   
ADD PARTY DemoCant's  
1: DemoCant's

Please insert a command: (VOTE [party], ADD PARTY [party], VOTES, PARTIES, EXIT, and HELP)   
ADD PARTY RepublicRats  
1: DemoCant's  
2: RepublicRats

Please insert a command: (VOTE [party], ADD PARTY [party], VOTES, PARTIES, EXIT, and HELP)   
VOTE DemoCant's  
VOTE DemoCant's 
Error: You can't add something to that  
1: DemoCant's has obtained 1 votes.  
1: DemoCant's  
2: RepublicRats

最佳答案

几乎所有 switch 案例中都缺少 break 语句,因此代码只会进入下一个案例。请参阅 docs 中有关秋季的部分。

case "VOTE": {
    ....
    break;
} 
.....

关于java - 用户输入和 Switch 语句的奇怪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27668349/

相关文章:

java - 如何在端口后的 URL 中使用应用程序名称(就像我们在 tomcat 部署的应用程序中所做的那样)来调用 GWT 应用程序中的其余 Web 服务?

java - "javax.swing.JPanel cannot be cast"异常,令人困惑

ios - 使用IF语句测试iOS Switch Control的ON/OFF设置

c - 程序无法识别字符

java - 在 Android 的 FrameLayout 上添加 Fragment

go - 在 Go 中根据字符串选择正确的导入

java - Netty 4.0.x 正确捕获 ConnectException

java - 写在文本文件上的文本或字符串与前一个词重叠。它应该逐行写每个字

Java 代理类型错误?

c# - 在 C# 中实现状态机的最佳方式(当性能很重要时)是什么?