2014-12-27 53 views
2
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); 
    }   
} 

我正在与一个奇怪的错误战斗。该程序读取用户输入并确定采用哪种操作,但通常情况下,交换机中的多个情况显然不应该触发。这让我很生气。有人知道它为什么这样做吗?用户输入和切换语句的奇怪错误

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 

回答

3

你错过了几乎所有switch案件break语句,这样的代码只是落在虽然到了下情况。参见秋季节,虽然在docs

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

非常感谢,我不知道! – 2014-12-27 14:53:47

3

case S IN一switch声明告吹,并继续执行下一个case。如果您不想让您的case跌破,请添加break;声明和case的结尾。例如:

switch (inputCommand){ 
    case "VOTE": 
     // code for voting 
     break; 
    case "ADD": { 
     // code for adding 
     break; 
    // etc... 
+0

谢谢,我不知道。 – 2014-12-27 14:54:16

0

每个Case

后添加break声明如果你直到所有的情况下结束或直到一个break遇到

不添加 break语句的代码流将进入下一个case
+0

@Hans van der Laan如果有帮助的话就投票赞成 – Abhishek 2014-12-27 14:56:52

2

我一个奇怪的错误战斗。

这就是为什么在提出问题之前使用调试器的原因。

该程序读取用户输入,并确定采取哪种操作,而且通常情况下,交换机中的多个案例显然不应该被触发。

因为没有break;return;没有理由突破交换机。

注意:Java,C,C++,C#都是这样做的。

这让我很生气。有人知道它为什么这样做吗?

因为这就是它应该做的。