1

列の2次元配列の行の合計を右側に追加する必要があります。ただし、コードを実行すると、最後に到達するまで、すべての列のすべての行の合計が追加されます。このように: http://i.imgur.com/7CCPu.png合計は正しいですが、1 行に 1 回だけ下がると想定されています。

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

}

4

1 に答える 1

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
}
于 2012-10-09T17:17:55.687 に答える