0

私はこのクラスを持っており、printVotes メソッドでは、各投票を出力するために毎回 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;
        }
    }
}
4

2 に答える 2

1

a を渡し、List<Candidate> candidates;各候補が を持っていると仮定するとList<Integer> Votes:

    List<Integer> votes= new ArrayList<Integer>() ;
    for(Candidate c:candidates)
    {
       votes.add(c.GetVote()) ;
    } 
    for(Integer v:votes)
    {
      System.out.println(v);
    }
于 2013-05-04T21:55:34.450 に答える