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