0

わかりました、2次元配列の列の合計を追加しようとしていますが、これまでのところ、行の合計の合計を追加できましたが、他の3列は追加できませんでした。誰かが私が間違っていることを教えてもらえますか?

ここに私の出力の写真があります

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

}

4

2 に答える 2

1

問題を解決するためのいくつかの簡単な変更:

1> 最初の 2 つの 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. 以下のように、TotalSum の隣に新しい colSum[] を定義します。

    int TotalSum = 0;
    int colSum[] = new int[]{0,0,0};
    
  2. ループの投票出力を更新して、列の合計も行います。

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

お役に立てれば!!

于 2012-10-13T04:32:59.503 に答える
0

あなたの論理は少しずれています。

最初の for ループの後:

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

ファイルの最初の 50 行を既に使用しており、"states" 配列の各要素が各行のテキストを保持しています。次回スキャナーから読み取ろうとすると、最初に述べたように、ファイルの下部にある総計のみを読み取るため、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 ..
于 2012-10-13T04:50:58.327 に答える