Java 2维数组矩阵程序

huangapple go评论70阅读模式
英文:

Java 2d array matrix program

问题

int even = 0;
int odd = 0;
for(i = 0; i < 5; i++){
    for(j = 0; j < 5; j++){
        if(j % 2 >= i)
            even += twoD[i][j];
        else
            if(i % 2 != j)
                odd += twoD[j][i];
    }
}
System.out.println("The sum of the even elements above the diagonal is: " + even);
System.out.println("The sum of the odd elements below the diagonal is: " + odd);

这是我正在处理的代码。问题是它没有按照我想要的方式显示对角线上方和下方的元素总和。矩阵基本上是随机的,所以我不得不每次都检查它以验证总和。

英文:
int even = 0;
int odd = 0;
for(i=0;i&lt;5;i++){
	for(j=0;j&lt;5;j++){
		if(j%2&gt;=i) // I think the problem is this
			even += twoD[i][j];
		else
			if(i%2!&gt;=j) // I think the problem is this
				odd += twoD[j][i];
	}
}
System.out.println(&quot;The sum of the even elements above the diagonal is: &quot;+even);
System.out.println(&quot;The sum of the odd elements below the diagonal is: &quot;+odd);

This is the code I am working with. The problem is it doesn't display the real sum of the elements above and below as I'd want in a diagonal. The matrix is basically randomized so I had to check it everytime to verify the sum.

答案1

得分: 0

确切如你所想,条件是不正确的。实际上我不确定背后的逻辑是什么。
你需要做的是判断给定的数字是在对角线之上还是之下。你可以通过比较索引本身来做到这一点 - j>ij<i
然后你只需要检查给定的数字(而不是索引)是奇数还是偶数 - twoD[i][j] % 2 == 0

类似下面这样的代码应该能完成任务:

for(i=0; i<5; i++){
    for(j=0; j<5; j++){
        if(j>i && twoD[i][j]%2==0){
           even += twoD[i][j];
        }
        if(j<i && twoD[i][j]%2!=0){
           odd += twoD[i][j];
        }
    }
}
英文:

Exactly as you've thought, the conditions are incorrect. Actually I'm not sure what was the logic behind them.
What you need to do is to find whether given number is above or below diagonal. You can do that by comparing indicies themselves - j&gt;i and j&lt;i.
Only then you need to check if a given number (not index) is odd or even - twoD[i][j] % 2 == 0.

Something like so should get the job done:

for(i=0;i&lt;5;i++){
    for(j=0;j&lt;5;j++){
        if(j&gt;i &amp;&amp; twoD[i][j]%2==0){
           even += twoD[i][j];
        }
        if(j&lt;i &amp;&amp; twoD[i][j]%2!=0){
           odd += twoD[i][j];
        }
    }
}

答案2

得分: 0

对于对角线上方的元素 j > i(列的索引始终大于行的索引);对于对角线下方的元素 j < i

然后,循环可以稍微优化,以提供 j > i 并交换矩阵对角线以下元素的索引:

int even = 0;
int odd = 0;
for(int i=0; i<twoD.length; i++) {
    for(int j=i + 1; j<twoD[i].length; j++) {
        if(twoD[i][j] %2 == 0) {
            even += twoD[i][j];
        }
        if(twoD[j][i] %2 != 0) {  // 交换对角线以下元素的索引
            odd += twoD[j][i];
        }
    }
}
英文:

For the elements above the diagonal j &gt; i (index of column is always greater than that of the row); for the elements below j &lt; i.

Then the loops may be optimized slightly to provide j &gt; i and swapping indexes for elements below the matrix diagonal:

int even = 0;
int odd = 0;
for(int i=0; i&lt;twoD.length; i++) {
	for(int j=i + 1; j&lt;twoD[i].length; j++) {
	    if(twoD[i][j] %2 == 0) {
		    even += twoD[i][j];
		}
		if(twoD[j][i] %2 != 0) {  // swap indexes below diagonal
		    odd += twoD[j][i];
		}
    }
}

huangapple
  • 本文由 发表于 2020年9月29日 14:53:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64114343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定