我有这个类,并且在printVotes方法中,我不得不每次打印每个投票的if语句。是否有任何方法来结合两个if语句。我可以同时打印候选人的所有姓名和得票数吗?如何在一个循环中结合if语句
public class TestCandidate {
public static void main(String[] args)
{
Canidate[] canidate = new Canidate[5];
// create canidate
canidate[0] = new Canidate("John Smith", 5000);
canidate[1] = new Canidate("Mary Miller", 4000);
canidate[2] = new Canidate("Michael Duffy", 6000);
canidate[3] = new Canidate("Tim Robinson", 2500);
canidate[4] = new Canidate("Joe Ashtony", 1800);
printVotes(canidate) ;
}
public static void printVotes(Canidate [] List)
{
double max;
int index;
if (List.length != 0)
{
index = 0;
for (int i = 1; i < List.length; i++)
{
}
System.out.println(List[index]);
}
if (List.length != 0)
{
index = 1;
for (int i = 1; i < List.length; i++)
{
}
System.out.println(List[index]);
return;
}
}
}
最好不要在Java中使变量名大写。特别是因为有这么多'List'类型。 – squiguy 2013-05-04 21:51:40
为什么不使用for循环循环5次'printVotes(canidate)'?每次发送一个不同的数组索引值。而不是多次循环遍历你的方法。 – Tdorno 2013-05-04 21:52:53
两种控制流程有什么区别? – 2013-05-04 21:52:57