java - Java String Switch 计票
问题描述
我已经查看了几个将 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);
}
}
解决方案
核心问题归结为这段代码......
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);
这不是您String
在 Java 中进行比较的方式,您应该使用!"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;
}
推荐阅读
- ruby-on-rails - 恢复数据库后,从相同的 SGID 加载 ActionText::Attachable 不再有效
- javascript - 使用 javascript 抓取网页 - 由于错误 80020101 无法完成操作
- ssh - gitlab runner ssh私钥644文件权限错误
- angular - ngmodel没有在茉莉花测试中填充输入元素值?
- r - R:一个时间间隔是否落在另一个时间间隔内?
- ansible - 是否可以将状态用于ansible?
- c# - 在 C# 字典中添加和增加键和值
- c# - Selenium C#:如何在
标签后获取文本 - javascript - 检查常用密码的最佳方法?(最佳实践和性能明智)
- android - rawQuery() 总是立即执行,不管实际查询需要多长时间