好吧,我想添加一個二維數組的列的總和,但到目前爲止,我能夠將總和的行數但不是其他3列。有人能告訴我我做錯了什麼嗎?如何總結在java中的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][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);
}
}
請張貼[SSCCE(http://sscce.org/)文本數據,而不是代碼段與您的IDE的截圖! –
看起來您要求我們編寫代碼來完成作業問題。 「對不起,不行!」 –