2012-10-13 74 views
0

好吧,我想添加一個二維數組的列的總和,但到目前爲止,我能夠將總和的行數但不是其他3列。有人能告訴我我做錯了什麼嗎?如何總結在java中的2維數組的列

here is picture of my output

public static void main(String[] args) throws IOException 
{ 
    // TODO code application logic here 
    File election = new File("voting_2008.txt"); 
    Scanner sc = new Scanner(election); 

    String[] states = new String[51]; 
    int[][]votes = new int[51][3]; 


    for (int s=0; s < 51; s++) 
    { 
     states[s] = sc.nextLine(); 
    } 

    for(int c=0; c < 3; c++) 
    { 
     for(int s=0; s < 51; s++) 
     { 
      votes[s][c] = sc.nextInt(); 

     } 

    } 
    Formatter fmt = new Formatter(); 
    fmt.format("%20s%12s%12s%12s%21s", "State", "Obama", "McCain", "Other", "Total by state"); 
    System.out.println(fmt); 
    int TotalSum; 
    TotalSum = 0; 
    for (int s=0; s < 51; s++) 
    { 
     fmt = new Formatter(); 
     fmt.format("%20s", states[s]); 
     System.out.print(fmt); 
     for(int c=0; c < 3; c++) 
     { 
      fmt = new Formatter(); 
      fmt.format("%12d", votes[s][c]); 
      System.out.print(fmt); 

     } 


      int sum =0; 
      for (int col=0; col < votes[s].length; col++) 
      { 
       sum = sum + votes[s][col]; 

      } 

      TotalSum += sum; 
      fmt = new Formatter(); 
      fmt.format("%21d", sum); 
      System.out.print(fmt); 



     System.out.println(); 

    } 
    Formatter fmt2 = new Formatter(); 
    fmt2.format("%20s%12s%12s%12s%21s", "Total", "", "", "", TotalSum); 
    System.out.print(fmt2); 
} 

}

+3

請張貼[SSCCE(http://sscce.org/)文本數據,而不是代碼段與您的IDE的截圖! –

+1

看起來您要求我們編寫代碼來完成作業問題。 「對不起,不行!」 –

回答

1

一些簡單的改變來解決你的問題:

1>合併前兩個for循環爲:

for (int s=0; s < 2; s++){ 
     states[s] = sc.next(); 
     for(int c=0; c < 3; c++) { 
      votes[s][c] = sc.nextInt(); 
     } 
    } 
  1. 定義一個新的colSum []旁邊TotalSum如下:

    int TotalSum = 0; 
    int colSum[] = new int[]{0,0,0}; 
    
  2. 更新票打印的for循環做列總和,以及:

    for(int c=0; c < 3; c++) { 
         colSum[c]+=votes[s][c]; // <-- new line to do the column sum 
         fmt = new Formatter(); 
         fmt.format("%12d", votes[s][c]); 
         System.out.print(fmt); 
        } 
    
  3. 打印的列在最後一行總結爲:

    fmt2.format("%20s%12s%12s%12s%21s", "Total", colSum[0], colSum[1], colSum[2], TotalSum); 
        System.out.print(fmt2); 
    

希望這有助於!

0

你的邏輯有點偏離。

後,你的第一個for循環:

for (int s=0; s < 51; s++) 
{ 
    states[s] = sc.nextLine(); 
} 

你已經消耗的文件中第50行,每個元素的「狀態」數組中保存每個行的文本。下次您嘗試從掃描儀讀取數據時,您已經位於第51行,因此您只能讀取文件底部的總數,就像您最初所說的那樣。

我認爲你想要做的是爲每個狀態(文件中的每一行)總結數字列1,2和3?

下面是一些示例代碼,應該有所幫助:

File election = new File("voting_2008.txt"); 

Scanner scanner = new Scanner(election); 

String[] states = new String[50]; // 50 united states 
int[] votes = new int[50]; 

// only grab the first 50 lines, each line a different state 
// assuming file layout is: stateName,count1,count2,count3 
for (int i = 0; i < 51; i++) { 

    states[i] = scanner.next(); 

    // grab the next 3 numbers 
    int voteTotal = 0; 
    for (int j = 0; j < 3; j++) { 
     voteTotal += scanner.nextInt(); 
    } 
    votes[i] = voteTotal; // total votes for this state 

} 

// ... display results ..