所以我在printResults
课程中遇到TestCandidate
方法时遇到问题。一切似乎按预期运行,除了显示每个候选人收到的总票数百分比的列。出于某种原因,该操作仅打印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 :(得分:3)
分数的十进制表示是0到1之间的数字,整数不能表示。此外,它不是一个百分比,因为你没有乘以100。
您可以在分割前乘以100,或者可以将分数的底部强制转换为(double)
,以便在乘以100之前保留小数位。
使用int
对于大数字来说可能存在风险 - 假设候选人获得了3000万票,乘以100会将其包装成负数,因为int
不能代表30亿
答案 1 :(得分:2)
(double)list[x].getVotes()/getTotal(list)*100
答案 2 :(得分:2)
要正确显示百分比,请使用以下代码行:
(double)100*list[x].getVotes()/getTotal(list)
这是因为Java整数除法为您提供了在结果中看到的所有零(0%):