编java程序求解n阶魔方矩阵代码
1个回答
展开全部
import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入n*n数组,n=");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] a = new int[n][n];//定义n*n数组
int result = 0;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
System.out.println("请输入第"+i+"行,第"+j+"列元素:");
a[i][j]=sc.nextInt();
}
result +=a[i][i];
}
System.out.println("对角线元素和为:"+result);
}
}
public class Matrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入n*n数组,n=");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] a = new int[n][n];//定义n*n数组
int result = 0;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
System.out.println("请输入第"+i+"行,第"+j+"列元素:");
a[i][j]=sc.nextInt();
}
result +=a[i][i];
}
System.out.println("对角线元素和为:"+result);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |