//求数组中最大的数: import java.util.Scanner; class Max { public static void Max(int s[][],int m,int n) { int max = s[0][0]; int t; System.out.println(); for(m=0;m<3;m++) { for(n=0;n<5;n++) { if(s[m][n]>max) { max = s[m][n]; } } } System.out.println("数组中最大的元素是:"); System.out.println(max); } public static void main(String[] args) { int i; int j = 0; int[][] a = new int[3][5]; Scanner in = new Scanner(System.in); System.out.println("请输入一个3行5列的数组:"); for(i=0;i<3;i++) { for(j=0;j<5;j++) { a[i][j] = in.nextInt(); } } Max(a,i,j); } }
//求n阶矩阵对角线元素的和: import java.util.Scanner; class Sum { public static void sum(int c[][],int m) { int i,s = 0; for(i=0;i<m;i++) s = s+c[i][i]+c[i][m-i-1]; if(m%2==0) System.out.println("对角线元素之和为:"+s); else { s = s - c[m/2][m/2]; System.out.println("对角线元素之和为:"+s); } } public static void main(String[] args) { int i,j; Scanner in = new Scanner(System.in); System.out.println("请输入n阶矩阵的阶数:"); int n = in.nextInt(); int b[][] = new int[n][n]; System.out.println("请输入n行n列数组:"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { b[i][j] = in.nextInt(); } } sum(b,n); } }
时间: 2024-11-02 11:57:05