为什么我的投票模拟器无法正常工作?

时间:2017-09-08 16:10:26

标签: java

对于一项学校作业,我制作了一个投票机,对投票进行计数并将其打印出来。这是VotingMachine类及其所有方法。

public class VotingMachine {
//declaring instance variables
private int democratVotes;
private int republicanVotes;
private int independentVotes;
//default constructor
public VotingMachine(){
}

//parameter constructor for changing values
public VotingMachine(int demVotes, int repubVotes, int indeVotes){
this.democratVotes = demVotes;
this.republicanVotes = repubVotes;
this.independentVotes = indeVotes;
}
//getter methods
public int getDemocratVotes(){
return democratVotes;
}
public int getRepublicanVotes(){
return republicanVotes;
}
public int getIndependentVotes(){
return independentVotes;
}
//setter methods
public void setDemocratVotes(int votes){
this.democratVotes = votes;
}
public void setRepublicanVotes(int votes){
this.republicanVotes = votes;
}
public void setIndependentVotes(int votes){
this.independentVotes = votes;
}
//increment votes by 1 methods
public void voteDemocrat(){
democratVotes += 1;
}
public void voteRepublican(){
republicanVotes += 1;
}
public void Independent(){
independentVotes += 1;
}
//overloaded methods (methods with same names as above)
public void voteDemocrat(int votes){
democratVotes += votes;
}
public void voteRepublican(int votes){
republicanVotes += votes;
}
public void voteIndependent(int votes){
independentVotes += votes;
}
//method to clear all votes
public void clearVote(){
democratVotes = 0;
republicanVotes = 0;
independentVotes = 0;
}
//method to calculate total votes
public int totalVotes(){
int totalVotes = democratVotes + republicanVotes + independentVotes;
return totalVotes;
}
//methods to calculate percentages while calling totalVotes()
public double percentDemocrat(){
double percentDemocrat = democratVotes / totalVotes();
return percentDemocrat;
}
public double percentRepublican(){
double percentRepublican = republicanVotes / totalVotes();
return percentRepublican;
}
public double percentIndependent(){
double percentIndependent = independentVotes / totalVotes();
return percentIndependent;
}

//method to print statistics
public void percentStats(){
System.out.println("Democrats: " + democratVotes);
System.out.println("Republicans: " + republicanVotes);
System.out.println("Independent: " + independentVotes);
System.out.println("Total Votes: " + totalVotes());
System.out.println("Percent Democrat Votes: " + percentDemocrat());
System.out.println("Percent Republican Votes: " + percentRepublican());
System.out.println("Percent Independent Votes: " + percentRepublican());
}
}

这是我用于项目的驱动程序:

public class MachineDriver {

    public static void main(String[] args) {
        VotingMachine test = new VotingMachine();
        test.voteDemocrat();
        test.voteDemocrat();
        test.voteRepublican();
        test.voteIndependent();
        test.percentStats();
    }
}

但是,当我运行程序时,percentDemocrat,percentRepublican和percentIndependent都显示为0.0。我做错了什么?以下是控制台中显示的内容:

民主党人:2 共和党人:1 独立:1 总票数:4 民主党投票率:0.0 共和党票数百分比:0.0 独立票数百分比:0.0

1 个答案:

答案 0 :(得分:1)

您正在将整数除以“结果等于零”的原因。

您可以将整数转换为双精度

public double percentDemocrat(){
    double percentDemocrat = ((double)democratVotes) / totalVotes();
    return percentDemocrat;
}