[英]Sum of columns of 2d array
這是我的代碼:
static void Display (int[] [] array, int[] count)
{
System.out.println ("\n\t Original Order");
for (int i = 0 ; i < 3 ; i++)
{
int count1 = 0;
for (int j = 0 ; j < 4 ; j++)
{
System.out.print (array [i] [j] + "\t");
count1 = count1 + array [i] [j];
count [i] = count [i] + array [j] [i];
}
System.out.print (" " + count1);
System.out.println ();
}
System.out.println ();
for (int i = 0 ; i < 4 ; i++)
{
System.out.print (count [i] + "\t");
}
}
如果將數組“ count”初始化為4長,並且所有點都填充為0,則直接使用變量將其寫入數組“ array”。 然后,當您運行此命令時,它將起作用,除了最后一列(上下)僅為0的事實。它實際上不會顯示最后一列的總和。 只是想知道是否有人知道為什么它不起作用會非常有幫助,謝謝!
看來您可能已經在代碼的一部分中翻轉了索引:
count [i] = count [i] + array [j] [i];
我想你的意思是:
count [i] = count [i] + array [i] [j];
編輯:給出您的評論,實際的問題是,因為您正在通過這樣做遍歷列:
count [i] = count [i] + array [j] [i];
...當您的數組長度為4時,變量i
僅會變為3。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.