当前位置:网站首页>Reshape a two-dimensional array with 3 rows and 3 columns to find the sum of the diagonals

Reshape a two-dimensional array with 3 rows and 3 columns to find the sum of the diagonals

2022-06-26 10:21:00 m0_ sixty-one million nine hundred and sixty-one thousand eight

public static void main(String[] args) {
		
		int sum = 0;
	int z [][]=new int [3][3];
z[0] = new int [] {1, 2, 3};
z[1] = new int [] {4, 5, 6};
z[2] = new int [] {7, 8 ,9};
for (int i=0 ;i<3;i++) {
	for (int j=0; j<3;j++) {
		if (i ==j) {
			sum = sum + z[i][j];
		
	
		}
    }
}	
System.out.println( sum);
	}
}

The result is 15

原网站

版权声明
本文为[m0_ sixty-one million nine hundred and sixty-one thousand eight]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260930071393.html