java - 投票系统错误
问题描述
所以,目前我的投票系统运行良好,但现在我得到了除以零代码:/。这不应该发生,因为“总整数总是有一个大于零的数值(除非我错过了什么?)。无论如何,我希望程序能够投票并将他们组织为女性和男性选民,以及他们是否投票特朗普或克林顿。一揽子投票;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class vote {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Declare Strings and Initialize
String PCandidate1=null;
String Gender1=null;
String Economy1= null;
//Declare Integer
int Age;
int Trump=0;
int Clinton=0;
int Male=0;
int Female=0;
int MaleT=0;
int FemaleT=0;
int MaleC=0;
int FemaleC=0;
int Young=0;
//all data read from a dialog box comes into a string
String svalue,output ="";
//Need to Implement While loop to keep collecting data
int counter=0;
while(counter<4) {
counter++;
//Vote for Presidential Candidate and Tally the Votes!
svalue =JOptionPane.showInputDialog(null,"Will you Vote for Clinton or Trump?","Input Data", JOptionPane.QUESTION_MESSAGE);
svalue=JOptionPane.showInputDialog(null,"Are you Male or Female (M/F)?","Input Data", JOptionPane.QUESTION_MESSAGE);
if (svalue.equals("trump")||(svalue.equals("Trump")))
{
if (svalue.equals("F")||(svalue.equals("f")))
Trump++;
Female++;
FemaleT++;}
if (svalue.equals("Trump")||(svalue.equals("trump")))
{
if (svalue.equals("M")||(svalue.equals("m")))
MaleT++;
Trump++;
Male++;}
if (svalue.equals("clinton")||(svalue.equals("Clinton"))){
if (svalue.equals("M")||(svalue.equals("m")))
Clinton++;
MaleC++;
Male++;}
if (svalue.equals("Clinton")||(svalue.equals("clinton"))){
if(svalue.equals("F")||(svalue.equals("f")))
FemaleC++;
Clinton++;
Female++;}
PCandidate1= (svalue);
//Inpute Users Age
svalue=JOptionPane.showInputDialog(null,"What is Your Age?","Input Data", JOptionPane.QUESTION_MESSAGE);
Age=Integer.parseInt(svalue);
if (Age<=25)
Young++;
// Get Users Input about State of the Economoy
svalue=JOptionPane.showInputDialog(null,"Do you feel the economy is getting better?","Input Data", JOptionPane.QUESTION_MESSAGE);
Economy1= (svalue);
}
int Total=Male+Female;
//Output The Data to Form
output=output+"Users Voted For Trump: " + Trump +"\n"
+ "Users Voted For Clinton: " + Clinton+ "\n"
+"Total Users Polled: " +(Total)+"\n"
+ "Male Voters: " + Male+ "\n"
+ "Female Voters: " + Female+ "\n"
+ "% Female Voters for Clinton: " + (FemaleC/Clinton)*100+ "\n"
+ "% Male Voters for Clinton: " + (MaleC/Clinton)*100+ "\n"
+ "% Female Voters for Trump: " + (FemaleT/Total)*100+ "\n"
+ "% Male Voters for Trump: " + (MaleT/Total)*100+ "\n"
+ "Number of Young People Polled: " + Young+ "\n"
;
//write all in a dialog box
JOptionPane.showMessageDialog(null,
output,"Output:",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
解决方案
你有很多问题if-statements
:
if (svalue.equals("clinton")||(svalue.equals("Clinton"))){
if (svalue.equals("M")||(svalue.equals("m")))
Clinton++;
MaleC++;
Male++;}
- 第二个 if 语句中没有括号。
- 您
if's
检查一个值是否等于“Clinton”和“m”。这意味着您的第二个if-statement
将永远不会被输入。所以Clinton
永远不会更新,永远为零。(我们认为是俄罗斯人..)
您需要有第二个变量来判断选民是男性还是女性,并在第二个变量中检查它if-statement
而不是覆盖svalue
旁注:适当的缩进将帮助您发现这些错误
所以更像这样:
gender =JOptionPane.showInputDialog(null,"Are you Male or Female (M/F)?","Input Data", JOptionPane.QUESTION_MESSAGE);
//…
if (svalue.equals("clinton")||(svalue.equals("Clinton"))){
if (gender.equals("M")||(gender.equals("m"))) {
Clinton++;
MaleC++;
Male++;
}
//Etc
推荐阅读
- javascript - 使用javascript时刻将值转换为秒
- swift - 使用完成块的自定义意图处理程序
- flutter - Flutter 增加 ListTile 高度
- terraform - 有什么方法可以通过 Terraform 在 Azure SQL 中配置“高级数据安全”选项?
- regex - re2:在成对分隔符之间提取多个字段
- r - 在 R 中从 Bloomberg 下载多个时间序列的干净方式
- mongodb - Mongoose-如何在填充和过滤后使用“where”子句
- apache-kafka - 如何在单个 kafka 主题的所有分区中写入相同的消息?
- python - Django:无法找到静态文件夹
- php - 无法从控制器传递变量以在 CodeIgniter 中查看