编程求一个4x4数组的左下三角(包括主对角线)元素的和。
1个回答
展开全部
import java.util.Random;
public class PlanarArraySum {
public static void main(String[] args) {
int[][] planar = new int[4][4];
Random rnd = new Random();
System.out.println("二维数组:");
for (int i = 0; i < planar.length; i++) {
for (int j = 0; j < planar[i].length; j++) {
planar[i][j] = rnd.nextInt(100);
System.out.print(planar[i][j] < 10 ? " " + planar[i][j] : " " + planar[i][j]);
}
System.out.println();
}
int sum = 0;
int k = 0;
for (int i = planar.length - 1; i >= 0; i--) {
for (int j = k; j < planar[i].length; j++) {
sum += planar[i][j];
}
k++;
}
System.out.println("左下三角和:" + sum);
}
}
public class PlanarArraySum {
public static void main(String[] args) {
int[][] planar = new int[4][4];
Random rnd = new Random();
System.out.println("二维数组:");
for (int i = 0; i < planar.length; i++) {
for (int j = 0; j < planar[i].length; j++) {
planar[i][j] = rnd.nextInt(100);
System.out.print(planar[i][j] < 10 ? " " + planar[i][j] : " " + planar[i][j]);
}
System.out.println();
}
int sum = 0;
int k = 0;
for (int i = planar.length - 1; i >= 0; i--) {
for (int j = k; j < planar[i].length; j++) {
sum += planar[i][j];
}
k++;
}
System.out.println("左下三角和:" + sum);
}
}
更多追问追答
追问
C语言啊……
追答
。。。。。。。天天泡在Java里,你向我提问,我还以为你问的是Java,C语言的思路也一样,照着来就行。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询