0

好的,我正在尝试添加二维数组的列的总和,但到目前为止,我能够添加行的总和,但不能添加其他 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> 将前两个 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 回答