gpt4 book ai didi

java - 帮助java矩阵?

转载 作者:行者123 更新时间:2023-12-01 17:39:02 25 4
gpt4 key购买 nike

我需要帮助,我遇到了这个矩阵的问题,我需要以这种方式得到它:

1 2  3  4 = 102 4  6  8 = 203 6  9 12 = 304 8 12 16 = 40

But I have it in this way:

1 2 3 4 12 4 6 8 23 6 9 12 34 8 12 16 4

I dont know how can we do that, I intent but nothing

This is my code:

public class Matrix {

public static void main(String args[]) {

int mult = 4;


for (int i = 1; i <= mult; i++) {
// System.out.println();


for (int j = 1; j <= mult; j++) {
int operacion = i * j;
int suma = 0;
suma = operacion + suma;

System.out.print(operacion + " ");

}


int sum = 0;
sum = i + sum;
System.out.println(sum);


}
}
}

再见

最佳答案

当你这样做时:

for (int j = 1; j <= mult; j++) {
int operacion = i * j;
int suma = 0;
suma = operacion + suma;

suma 始终等于 operacion,因为您每次都将其设置为 0,然后添加 operacion

您想要执行此操作:

int suma = 0;
for (int j = 1; j <= mult; j++) {
int operacion = i * j;
suma += operacion;

关于java - 帮助java矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3240136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com