2011-06-03 27 views
0

我需要找出每個人有多少票。我有一個數字來計算有多少個候選人在場,這個候選人的數量是5.現在我有一大堆來自數組列表的數字,我需要計算每個候選人的快樂投票數以及候選人數的變化取決於我使用的數組列表,但是用於測試目的,我使用TEST5其中有5追蹤多少個數字和計數器

//Numbers from arraylist: 
1 
1 
2 
5 
2 
5 
5 
5 

,這是我的代碼

// My code 
for (int i=1; i <output.size(); i++){ 
     char checkBallot = output.get(i).charAt(1); 
     //candidate is array to track how many output as what 
        // output is array of numbers I want to count. 
     String aString = Character.toString(checkBallot); 
     int c = Integer.parseInt(aString); 
     int b = c; 
     for (int p= 0; p < count; p++){ 
      if(c == p+1){ 
       int oldvalue =0; 
       candidate[c] = c+ oldvalue ; 
       oldvalue = c ; 
       System.out.println("this is the counter:" +c); 
      } 

     } 
+1

問題是什麼嗎? – 2011-06-03 00:33:08

+2

另外,粘貼代碼的其餘部分...輸出是什麼?州?候選人? etc – Bohemian 2011-06-03 00:35:35

+0

輸出是從數組列表狀態的數字是它現在狀態的順序是1。候選者是一個數組,用於跟蹤輸出中有多少個1 2 3 4 5。對不起,我寫我想達到的東西太可怕了。 – 2011-06-03 00:51:59

回答

2

我想你應該擺脫oldvalue和使用++candidate[c];,而不是那些三行。假設我理解你的代碼,而我幾乎沒有。

+0

對不起,我是一個初學者,我仍然試圖獲得它的竅門...... – 2011-06-03 01:42:23

0

要數1,2,3,4,5究竟有多少在輸出數組列表:如果你有一個整數的ArrayList:

final int[] candidate = new int[count]; 
for(int i : output) 
    candidate[i]++; 

假設「數」是最大的價值,你可能會在ArrayList中找到。

相反,如果你有一個字符串的ArrayList,那麼你就寫

final int[] candidate = new int[count]; 
for(String i : output) 
    candidate[Integer.parseInt(i)]++;