总结Java中的每一行和每一列
我不明白为什么数字加起来不正确。我试图把每一行和每一列加起来。我甚至不知道这些数字是从哪里来的。这是你的问题:总结Java中的每一行和每一列,java,arrays,row,multiple-columns,Java,Arrays,Row,Multiple Columns,我不明白为什么数字加起来不正确。我试图把每一行和每一列加起来。我甚至不知道这些数字是从哪里来的。这是你的问题: 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 sum+=sum+array[i][j]与sum=sum+sum+array[i][j]相同您添加了两次sum 应该
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
sum+=sum+array[i][j]与sum=sum+sum+array[i][j]相同您添加了两次sum
应该是:
总和=总和+数组[i][j];或sum+=数组[i][j]
如果只想打印每一行的总和,请在外部for循环中每次迭代将总和重置为0
然后在for循环的内部添加
所以最后你会有这个:
colSum[j]+=array[i][j];
这是你的问题:
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
sum+=sum+array[i][j]与sum=sum+sum+array[i][j]相同您添加了两次sum
应该是:
总和=总和+数组[i][j];或sum+=数组[i][j]
如果只想打印每一行的总和,请在外部for循环中每次迭代将总和重置为0
然后在for循环的内部添加
所以最后你会有这个:
colSum[j]+=array[i][j];
尝试初始化sum=0
对于列的总和,您可以这样做
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);
}
尝试初始化sum=0
对于列的总和,您可以这样做
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);
}
用i控制的外部循环对每行执行一次。如果要对一行中的所有数字求和,请在每个循环的开头将sum设置为零
如果你有sum=sum+x,那将把x加到sum中并存储在sum中。如果你写sum+=x,也会做同样的事情。但是您已经编写了sum+=sum+x,它将sum和x相加。由i控制的外部循环每行执行一次。如果要对一行中的所有数字求和,请在每个循环的开头将sum设置为零
如果你有sum=sum+x,那将把x加到sum中并存储在sum中。如果你写sum+=x,也会做同样的事情。但是您已经编写了sum+=sum+x,它将sum和x相加。您不必使用运算符+=,只需使用=。您不必使用运算符+=,只需使用=。您的for循环超出了范围。 请检查编译器: 输出:取出sum=sum+array[i][j] 行数=4cols=5 打印行总数=97 打印行数总和=206 打印行总数=334 打印行数总和=454 在这之后,它将工作,但它给出了所有行的总和,而不是打断它们
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;
}
}
}
中文:伪代码
添加一组行,然后停止。
r1
r2
r3
r4
或数组,如果需要r[x]
每次都要清理总和,而不是将它们相加。
所以需要4行和
和5列的和
c1
c2
c3
补体第四成份
碳五
所以你做了一个水平求和的循环
和一个垂直总和
也不要把你的名字写在作业的最上面。
修复后的输出
行数=4cols=5
打印行总数=97
打印行数总和=109
打印行数之和=128
打印行数总和=120
生成成功的总时间:0秒
现在您需要对列执行5次此操作,顺便说一句,您的for循环超出范围。 请检查编译器: 输出:取出sum=sum+array[i][j] 行数=4cols=5 打印行总数=97 打印行数总和=206 打印行总数=334 打印行数总和=454 在这之后,它将工作,但它给出了所有行的总和,而不是打断它们
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;
}
}
}
中文:伪代码
添加一组行,然后停止。
r1
r2
r3
r4
或数组,如果需要r[x]
每次都要清理总和,而不是将它们相加。
所以需要4行和
和5列的和
c1
c2
c3
补体第四成份
碳五
所以你做了一个水平求和的循环
和一个垂直总和
也不要把你的名字写在作业的最上面。
修复后的输出
行数=4cols=5
打印行总数=97
打印行数总和=109
打印行数之和=128
打印行数总和=120
生成成功的总时间:0秒
现在,您需要对列执行5次此操作,您不必使用运算符+=,只需使用= 。。。 实际上他需要+=但是他用错了 从逻辑上讲,他的代码是错误的 它应该是+=然后在外部for循环的每个实例之后,他应该将其切断
通过将值的总和再次重置为0来给出新值。您不必使用运算符+=,只需使用=
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
。。。
实际上他需要+=但是他用错了
从逻辑上讲,他的代码是错误的
它应该是+=然后在外部for循环的每个实例之后,他应该将其切断
通过将值的总和再次重置为0来给出新值
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
演示
演示
我明白了。但是这个公式会怎样改变来做这些列呢?@user3286008你想打印每个20,30,16,25,18,201924等等的总和吗?是的,对于20+30+16+20+25好的,我试过了,但出于某种原因,它告诉我们对于int I=0,j不能被解析;i<数组长度;i++{for int j=0;j
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] + " ");
}
}
}