所以我在使用 TestCandidate
类中的 printResults
方法时遇到了问题。一切似乎都按预期运行,除了显示每个候选人获得的总票数百分比的列。由于某种原因,该操作仅打印 0。 (郑重声明,当我使用“操作”一词时,我主要指的是 list[i].getVotes/getTotal(list)
操作。澄清一下)。
我已经尝试了用括号括起 list[i].getVotes
方法和 getTotal(list)
方法的各种组合,但无济于事。我也试过在 print 语句之外执行操作,只打印一个该操作分配给的变量。
奇怪的是,当我取出 list[i].getVotes
或 getTotal(list)
并只留下一个时,它们都打印得很好。只是不在一起,我只是想不通为什么。
所以,总而言之,为什么 list[i].getVotes/getTotal(list)
不能正确划分? (除了问题要求之外,请不要尝试做任何事情,我想学习并修复自己的错误并尽可能地优化我的代码,但无论出于何种原因我都无法通过这个)。谢谢你!
这是 Candidate
类:
public class Candidate
{
private int votes;
private String candidateName;
public Candidate(String name, int numVotes)
{
votes = numVotes;
candidateName = name;
}
public String getName()
{
return candidateName;
}
public void setName(String name)
{
candidateName = name;
}
public int getVotes()
{
return votes;
}
public void setVotes(int numVotes)
{
votes = numVotes;
}
public String toString()
{
return candidateName + " received:\t" + votes + " votes.";
}
}
这是 TestCandidate
类:
public class TestCandidate
{
public static void main(String[] args)
{
Candidate[] election = new Candidate[5];
election[0] = new Candidate("John Smith", 5000);
election[1] = new Candidate("Mary Miller", 4000);
election[2] = new Candidate("Michael Duffy", 6000);
election[3] = new Candidate("Tim Robinson", 2500);
election[4] = new Candidate("Joe Ashtony", 1800);
printVotes(election);
System.out.println();
printResults(election);
System.out.println();
System.out.println("The total amount of votes cast was: " + getTotal(election) + ".");
}
public static <E> void printVotes(E[] array) //prints the candidates and the amount of votes they
{ //each received
for(E element : array)
{
System.out.printf("%s ", element);
System.out.println();
}
}
public static int getTotal(Candidate[] list) //adds up the votes from each candidate and prints the
{ //total amount
int totalVotes = 0;
for(int i = 0; i < list.length; i++)
{
totalVotes += list[i].getVotes();
}
return totalVotes;
}
public static void printResults(Candidate[] list) //prints a table containing candidate's name, amount of votes, and the
{ //percentage of total votes they earned
System.out.println("Candidate: \t Votes Recieved: \t Percent of Total Votes:");
for(int x = 0; x < list.length; x++)
{
System.out.println(list[x].getName() + " \t " + list[x].getVotes() + " \t \t \t " + list[x].getVotes()/getTotal(list) + "%");
}
}
}
这是我当前的输出:
John Smith received: 5000 votes.
Mary Miller received: 4000 votes.
Michael Duffy received: 6000 votes.
Tim Robinson received: 2500 votes.
Joe Ashtony received: 1800 votes.
Candidate: Votes Recieved: Percent of Total Votes:
John Smith 5000 0%
Mary Miller 4000 0%
Michael Duffy 6000 0%
Tim Robinson 2500 0%
Joe Ashtony 1800 0%
The total amount of votes cast was: 19300.
最佳答案
分数的十进制表示是介于 0 和 1 之间的数字,整数无法表示。另外,它不是百分比,因为您还没有乘以 100。
您可以在除法之前乘以 100,或者您可以将分数的底部转换为 (double)
以在乘以 100 之前保留小数位。
使用 int
可能对大数字有风险 - 假设候选人获得 3000 万张选票,乘以 100 会将其换成负数,因为 int
不能t代表30亿。
关于java - 为什么我的方法不能正确地返回除法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29319688/