2012-10-09 41 views
1

我必須在一列增加一個二維陣列的行的總和到右側。然而,當我運行代碼時,它會添加每列中所有行的總和,直到達到最後。是這樣的:http://i.imgur.com/7CCPu.png總和是正確的,但想下去,每行只有一次。如何在2維數組上正確使用格式化程序?

 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][4]; 
    int[] Totalbystate = new int[votes.length]; 

    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); 
    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); 

     } 

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

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


     System.out.println(); 
    } 
} 

}

+0

你同樣的問題又來了?你可能只是在那裏要求澄清..實際上你接受了那裏的答案.. –

+0

可能的重複[如何格式化格式化和格式化二維數組?](http://stackoverflow.com/questions/12804420/how格式化和格式化) –

+0

抱歉,用戶告訴我這是另一個問題 – UnPatoCuacCuac

回答

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

     } 
     fmt = new Formatter(); 
     fmt.format("%21d", sum); 
     System.out.print(fmt); // You are not printing newline after each vote.. 
    } 

使用嘗試: - System.out.println(fmt);代替註釋代碼..

更新: - 不要做上述變化 ..都不行..確實做了以下改變。

這裏是問題。你要打印你的total no of votes,51 * 51倍..你有這樣循環另一個循環,它已經運行了51次內部..

所以,解決您的問題 ..取下上面的代碼外循環,而只要保持這樣的: -

//for(int row=0; row < votes.length; row++) // Remove this loop 
    //{ // Remove this 

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

     } 
     fmt = new Formatter(); 
     fmt.format("%21d", sum); 
     System.out.print(fmt); 
    //} // Remove this also.. 

s你是從最外側的環中獲得價值..

更新: - 如果你想要的所有的選票。對於所有列總和..

int totalSum = 0; // Declare a variable outside it.. 
for (int s=0; s < 51; s++) // This is the place where the loop started 
{ 
    fmt = new Formatter(); 
    fmt.format("%20s", states[s]); 
    System.out.print(fmt); 

    // Other inner loops 

    int sum =0; // Your innerloop that calculates sum of each state.. 
    for (int col=0; col < votes[s].length; col++) 
    { 
     sum = sum + votes[s][col]; 

    } 

    totalSum += sum; // Add sum to `totalSum` 
    fmt = new Formatter(); 
    fmt.format("%21d", sum); 
    System.out.print(fmt); 

    // Continue with the outer loop 
} 
+0

當我刪除了循環正如你所說,變量行消失,這給了我「找不到符號」,「變行」的錯誤。謝謝。 – UnPatoCuacCuac

+0

@ user1663414 ..哦。對不起..我忘了提..代替'和's' ..最外層循環 –

+0

太感謝你了,那工作的變量row'。 – UnPatoCuacCuac