This question already has an answer here:
这个问题在这里已有答案:
- Why is the result of 1/3 == 0? 13 answers
为什么1/3 == 0的结果? 13个答案
For a school assignment I made a votingmachine that counts votes and prints them out. Here is the VotingMachine Class with all its methods.
对于一项学校作业,我制作了一个投票机,对投票进行计数并打印出来。这是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());
}
}
This is the driver that I am using for the project:
这是我用于项目的驱动程序:
public class MachineDriver {
public static void main(String[] args) {
VotingMachine test = new VotingMachine();
test.voteDemocrat();
test.voteDemocrat();
test.voteRepublican();
test.voteIndependent();
test.percentStats();
}
}
However, when I run the program the percentDemocrat, percentRepublican, and percentIndependent all show up as 0.0. What did I do wrong? Here is what shows up in the console:
但是,当我运行程序时,percentDemocrat,percentRepublican和percentIndependent都显示为0.0。我做错了什么?以下是控制台中显示的内容:
Democrats: 2 Republicans: 1 Independent: 1 Total Votes: 4 Percent Democrat Votes: 0.0 Percent Republican Votes: 0.0 Percent Independent Votes: 0.0
*党人:2名共和党人:1名独立人士:1总票数:4%*党人票数:0.0%共和党人票数:0.0%独立票数:0.0
1 个解决方案
#1
1
You are dividing integers that's why you getting result equal to zero.
你正在划分整数,这就是为什么你得到的结果等于零。
You can cast integer to double
您可以将整数转换为double
public double percentDemocrat(){
double percentDemocrat = ((double)democratVotes) / totalVotes();
return percentDemocrat;
}
#1
1
You are dividing integers that's why you getting result equal to zero.
你正在划分整数,这就是为什么你得到的结果等于零。
You can cast integer to double
您可以将整数转换为double
public double percentDemocrat(){
double percentDemocrat = ((double)democratVotes) / totalVotes();
return percentDemocrat;
}