总结Java中的每一行和每一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21641849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Summing up each row and column in Java
提问by user3286008
/*
* Programmer: Olawale Onafowokan
* Date: February 6, 2014
* Purpose: Prints the row and column averages
*/
class Lab4 {
public static void main(String[] args) {
int [][] scores = {{ 20, 18, 23, 20, 16 },
{ 30, 20, 18, 21, 20 },
{ 16, 19, 16, 53, 24 },
{ 25, 24, 22, 24, 25 }};
outputArray(scores);
}
public static void outputArray(int[][] array) {
int sum= 0;
int rowSize = array.length;
int columnSize = array[0].length;
System.out.println("rows=" + rowSize + "cols=" + columnSize);
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
sum += array[i][j];
}
System.out.println("Print the sum of rows = " + sum);
}
for (int i = 0; i < array.length; i++) {
sum = 0;
sum = sum + array[i][j];
// It is telling me the j can't be resolved
}
}
}
The program prints out:
程序打印出:
rows=4cols=5
Print the sum of rows = 612
Print the sum of rows = 20358
Print the sum of rows = 652058
Print the sum of rows = 20866609
I don't understand why it isn't adding up the numbers correctly. I am trying to add up each row and column. I am not even sure where these numbers are coming from.
我不明白为什么它没有正确地把数字加起来。我正在尝试将每一行和每一列相加。我什至不确定这些数字来自哪里。
采纳答案by Baby
Here is your problem:
这是你的问题:
sum += sum + array[i][j];
sum += sum + array[i][j]
is the same as sum = sum + sum + array[i][j]
(you added sum twice)
sum += sum + array[i][j]
与sum = sum + sum + array[i][j]
(你加了两次 sum)相同
Should be:
应该:
sum = sum + array[i][j];
Orsum += array[i][j];
sum = sum + array[i][j];
或者sum += array[i][j];
If you want to print the sum of each row only, reset your sum
to 0
for each iteration on outer for-loop
如果你要打印的每张的总和行只,重置sum
到0
上外每次迭代for-loop
for (int i = 0; i < array.length; i++){
sum=0;
....
If you want to print the sum of each column only, you need to add this:
如果只想打印每列的总和,则需要添加以下内容:
int[] colSum =new int[array[0].length];
Then inside for-loop
, add
然后在里面for-loop
,添加
colSum[j]+=array[i][j];
So finally you will have this:
所以最后你会得到这个:
int[] colSum =new int[array[0].length];
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
sum += array[i][j];
colSum[j] += array[i][j];
}
System.out.println("Print the sum of rows =" + sum);
}
for(int k=0;k<colSum.length;k++){
System.out.println("Print the sum of columns =" + colSum[k]);
}
回答by AAB
for (int i = 0; i < array.length; i++)
{
sum=0;
for (int j = 0; j < array[0].length; j++)
{
sum += array[i][j];
}
System.out.println("Print the sum of rows =" + sum);
}
try intializing sum =0;
尝试初始化 sum =0;
For Sum of Columns you could do this
对于列总和,您可以执行此操作
class Sample{
public static void main(String []args){
int arr[][]={{1,2,3},{1,2,3},{1,2,3}};
int sum=0;
for(int col=0;col<arr[0].length;col++){
for(int row=0;row<arr.length;row++){
sum+=arr[row][col];
}
System.out.println("Sum is "+sum);
sum=0;
}
}
}
回答by arcy
Your outer loop (controlled with i) executes once for each row. If you want to sum all the numbers in one row, set sum
to zero at the beginning of each loop.
您的外循环(由 i 控制)对每一行执行一次。如果要对一行中的所有数字求和,请sum
在每个循环开始时设置为零。
If you have "sum = sum + x", that will add x to sum and store in sum. If you write "sum += x", that will do the same thing. But you have written "sum += sum + x", which adds in sum AND x to sum.
如果您有“sum = sum + x”,则将 x 添加到 sum 并存储在 sum 中。如果你写“sum += x”,它会做同样的事情。但是你写了“sum += sum + x”,它把 sum AND x 添加到 sum 中。
回答by roy5150
You don't have to use the operator "+=", simply use "=".
您不必使用运算符“+=”,只需使用“=”。
回答by Geo
this for loop of yours is out of scope btw. check with the compiler:
顺便说一句,你的这个 for 循环超出了范围。检查编译器:
output : when taking out sum = sum + array[i][j]
输出:取出 sum = sum + array[i][j] 时
rows=4cols=5
Print the sum of rows = 97
Print the sum of rows = 206
Print the sum of rows = 334
Print the sum of rows = 454
rows=4cols=5
打印总行数 = 97
打印总行数 = 206
打印总行数 = 334
打印总行数 = 454
after that it would work but it gives sum of all rows not breaking them.
之后它会起作用,但它给出了所有行的总和而不破坏它们。
for (int i = 0; i < array.length; i++) {
sum = 0;
sum = sum + array[i][j];
// It is telling me the j can't be resolved
}
//j is out of scope there is no such thing as j in this loop
English: pseudo code
中文:伪代码
adding one set of row then stop.
r1
r2
r3
r4
or arrays if you want r[x]
each time cleaning the sum instead of adding them to each other.
so you need 4 sums of rows
and 5 sums of columns
c1
c2
c3
c4
c5
添加一组行然后停止。r1
r2
r3
r4
或数组,如果你想要 r[x]
每次清理总和而不是将它们彼此相加。
所以你需要 4 行总和
和 5 列总和
c1
c2
c3
c4
c5
so you do a loop for horizontal summation and one vertical summation.
所以你做一个水平求和和一个垂直求和的循环。
also don't put your name on top of the assignment.
output after fix
rows=4cols=5
Print the sum of rows = 97
Print the sum of rows = 109
Print the sum of rows = 128
Print the sum of rows = 120
BUILD SUCCESSFUL (total time: 0 seconds)
也不要把你的名字放在作业的顶部。
修复后输出
行=4cols=5
打印行总和= 97
打印行总和= 109
打印行总和= 128
打印行总和= 120
BUILD SUCCESSFUL(总时间:0 秒)
now you need to do this for columns 5 times
现在您需要对列执行此操作 5 次
回答by Geo
"you don't have to use the operator "+=", simply use "="."
“您不必使用运算符“+=”,只需使用“=”。
... actually he needs the += but then he is using it wrong.
...实际上他需要 += 但后来他用错了。
logically speaking his code is wrong.
从逻辑上讲,他的代码是错误的。
it should be += then after each instance of the outer for loop he should be cutting it off by resetting the sum of the value to 0 again to give out new values.
它应该是 += 然后在外部 for 循环的每个实例之后,他应该通过再次将值的总和重置为 0 以给出新值来切断它。
回答by Ram Pukar
public class Main {
public static void main(String[] args) {
ArrSort objArr = new ArrSort();
int[][] arr = {
{1,2,3},
{4,5,6},
{1,2,3}
};
objArr.getColsSum(arr);
}
}
class ArrSort {
public void getColsSum(int[][] arg){
int[] colsSum = new int[3];
colsSum[0] = 0;
colsSum[1] = 0;
colsSum[2] = 0;
for(int i = 0; i<arg.length; i++) {
for(int j = 0; j<arg[i].length; j++) {
System.out.print(arg[i][j] +" ");
if(j==0) {
colsSum[j]+=arg[i][j];
} else if(j==1) {
colsSum[j]+= arg[i][j];
} else {
colsSum[j]+= arg[i][j];
}
}
System.out.println();
}
for(int i = 0; i<colsSum.length; i++){
System.out.print(colsSum[i] + " ");
}
}
}