Java String Switch计票

时间:2018-06-25 00:12:28

标签: java

我看过几个将switch语句与字符串一起使用的示例,但尚未成功完成我的工作。我只是想计算A或B的票数,当我编译它时,它不会返回任何错误。但是,当我运行它时,出现运行时错误: 谁能指出我哪里出问题了?

import javax.swing.JOptionPane;

public class Votes {

   private String s_numVoters, singleVote;
   private static int v_numVoters = 0, v_countVotes = 0, aCount = 0, bCount = 0, invalidCount = 0;

   public int m_numVoters() {

      s_numVoters = JOptionPane.showInputDialog("How many voters will be voting today?");
      int v_numVoters = Integer.parseInt(s_numVoters);

      return v_numVoters;
   }

   public void countVotes(String[] votes) {
      do {

         singleVote = JOptionPane.showInputDialog("Voting system. Please, enter:\nA - To vote for candidate A.\nB - To vote for candidate B.\n"
                                                   + "Z - To finish the voting process.\nAnything else will be considered as an INVALID vote.");

         switch (votes[0]) {
            case "A":
               aCount = aCount + 1;
               break ;
            case "B":
               bCount = bCount + 1;
               break ;
            default :
               invalidCount = invalidCount + 1;
         }
      } while (singleVote != "Z" || (aCount + bCount + invalidCount) <= v_numVoters);
   }

   public static void main(String[] args) {

      Votes newVotes = new Votes();
      newVotes.m_numVoters();
      newVotes.countVotes(args);
      System.out.println(aCount + " " + bCount + " " + invalidCount);
   }
}

1 个答案:

答案 0 :(得分:2)

核心问题归结于这段代码...

public void countVotes(String[] votes) {
    do {

        singleVote = JOptionPane.showInputDialog("Voting system. Please, enter:\nA - To vote for candidate A.\nB - To vote for candidate B.\n"
                + "Z - To finish the voting process.\nAnything else will be considered as an INVALID vote.");

        switch (votes[0]) {
            case "A":
                aCount = aCount + 1;
                break;
            case "B":
                bCount = bCount + 1;
                break;
            default:
                invalidCount = invalidCount + 1;
        }
    } while (singleVote != "Z" || (aCount + bCount + invalidCount) <= v_numVoters);
}

public static void main(String[] args) {

    Votes newVotes = new Votes();
    newVotes.m_numVoters();
    newVotes.countVotes(args);
    System.out.println(aCount + " " + bCount + " " + invalidCount);
}

正在生成...

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at javaapplication195.Votes.countVotes(Votes.java:29)
    at javaapplication195.Votes.main(Votes.java:46)

“主要”原因是...

switch (votes[0]) {

因为您要传入的是args中的main,所以除非您实际输入了任何命令行参数,否则它将为空。

这引起了下一个问题,为什么!?您已经完成了忽略用户输入的操作

因此,上述内容可以改为...

public void countVotes() {
    do {
        // A local variable should be more useful here
        singleVote = JOptionPane.showInputDialog("Voting system. Please, enter:\nA - To vote for candidate A.\nB - To vote for candidate B.\n"
                + "Z - To finish the voting process.\nAnything else will be considered as an INVALID vote.");

        switch (singleVote) {
            case "A":
                aCount = aCount + 1;
                break;
            case "B":
                bCount = bCount + 1;
                break;
            default:
                invalidCount = invalidCount + 1;
        }
    } while (singleVote != "Z" || (aCount + bCount + invalidCount) <= v_numVoters);
}

public static void main(String[] args) {

    Votes newVotes = new Votes();
    newVotes.m_numVoters();
    newVotes.countVotes();
    System.out.println(aCount + " " + bCount + " " + invalidCount);
}

但是更好的解决方案是...

switch (singleVote) {
    case "A":
    case "a":
        aCount = aCount + 1;
        break;
    case "B":
    case "b":
        bCount = bCount + 1;
        break;
    default:
        invalidCount = invalidCount + 1;
}

或使用singleVote.toUpperCase()

singleVote = singleVote.toUpperCase()
switch (singleVote) {
    case "A":
        aCount = aCount + 1;
        break;
    case "B":
        bCount = bCount + 1;
        break;
    default:
        invalidCount = invalidCount + 1;
}

这将允许您修复代码中的下一个问题...

do {
    //...
} while (singleVote != "Z" || (aCount + bCount + invalidCount) <= v_numVoters);

这不是在Java中进行String比较的方式,应该使用!"Z".equals(singleVote)或类似的方法。

最后...

public int m_numVoters() {

    s_numVoters = JOptionPane.showInputDialog("How many voters will be voting today?");
    int v_numVoters = Integer.parseInt(s_numVoters);

    return v_numVoters;
}

您正在遮盖v_numVoters,这意味着您执行(aCount + bCount + invalidCount) <= v_numVoters时就是0

将其更改为更多...

public int m_numVoters() {

    s_numVoters = JOptionPane.showInputDialog("How many voters will be voting today?");
    v_numVoters = Integer.parseInt(s_numVoters);

    return v_numVoters;
}