2017.11.29_实验6数组


1.编写程序,用一维数组保存20个学生的某门课程的成绩,计算平均成绩,并输出。

package ex05;

public class AverageGrade {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double []a = { 100, 100, 100, 100, 100, 50, 50, 50, 50, 50, 100, 100,
				100, 100, 100, 50, 50, 50, 50, 50 };
		double sum = 0;
		for (int i = 0; i < a.length; i++) {
			sum += a[i];
		}
		System.out.println("20个学生的平均成绩为:" + sum / 20);
	}
}
/*20个学生的平均成绩为:75.0*/

2.对上题中数组中的数据进行从小到大排序(使用冒泡法和选择法)。

package ex05;

public class Sort {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double a[] = { 1, 3, 4, 5, 2 };
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + "  ");
		}
		System.out.println();
		for (int i = 0; i < a.length - 1; i++) {
			for (int j = 0; j < a.length - 1; j++) {
				if (a[j] > a[j + 1]) {
					double temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
		System.out.println("对a[]从小到大冒泡:");
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + "  ");
		}
		System.out.println();
		double b[] = { 3, 6, 9, 1, 0 };
		for (int i = 0; i < b.length; i++) {
			for (int j = i + 1; j < b.length; j++) {
				if (b[j] < b[i]) {
					double temp = b[i];
					b[i] = b[j];
					b[j] = temp;
				}
			}
		}
		System.out.println("对b[]从小到大选择排序:");
		for (int i = 0; i < b.length; i++) {
			System.out.print(b[i] + "  ");
		}

	}

}
/*
 1.0  3.0  4.0  5.0  2.0  
对a[]从小到大冒泡:
1.0  2.0  3.0  4.0  5.0  
对b[]从小到大选择排序:
0.0  1.0  3.0  6.0  9.0  
*/

3.

定义一个Student类,包括姓名、学号、成绩三个成员变量以及getName()、getNo()、getScore()三个方法。在构造函数中初始化姓名、学号、成绩三个成员变量。定义一个大小为20的类型为Student的数组,按学生成绩的高低,依次输出姓名和成绩。

package work03;

public class Student {

	private String name, no;
	private double score;

	public Student(String name, String no, double score) {
		super();
		this.name = name;
		this.no = no;
		this.score = score;
	}

	public String getName() {
		return name;
	}

	public String getNo() {
		return no;
	}

	public double getScore() {
		return score;
	}

	public void display() {
		System.out.println(name + " " + no + " " + score + " ");
	}

}


package work03;

import java.util.Scanner;

public class TestStudent {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student[] a = new Student[5];
		String name, no;
		double score;
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入:");
		for (int i = 0; i < 5; i++) {
			name = sc.next();
			no = sc.next();
			score = sc.nextDouble();
			a[i] = new Student(name, no, score);
		}
		for (int i = 0; i < 5; i++) {
			a[i].display();
		}
		Sortt.sort(a);
		System.out.println("按成绩降序排序:");
		for (int i = 0; i < 5; i++) {
			a[i].display();
		}
	}
}
/*
请输入:
小明  2017001  89
小李  2017002  60
小辉  2017003  78
小红  2017004  100
小华  2017005  92
小明 2017001 89.0 
小李 2017002 60.0 
小辉 2017003 78.0 
小红 2017004 100.0 
小华 2017005 92.0 
按成绩降序排序:
小红 2017004 100.0 
小华 2017005 92.0 
小明 2017001 89.0 
小辉 2017003 78.0 
小李 2017002 60.0 
*/
package work03;

public class Sortt {
	public static void sort(Student[] a) {
		Student temp;
		for (int i = 0; i < 5; i++) {
			for (int j = i + 1; j < 5; j++) {
				if (a[i].getScore() < a[j].getScore()) {
					temp = a[i];
					a[i] = a[j];
					a[j] = temp;
				}
			}
		}
	}
}

4.编程求一个3×3矩阵的主对角线元素之和及次对角元素之和。

package work03;

public class Sum {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[][] a = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
		getSum(a);

	}

	private static void getSum(int[][] a) {
		// TODO Auto-generated method stub
		double sum1 = 0;
		double sum2 = 0;
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				if (i == j) {
					sum1 += a[i][j];
				}
				if (j == a.length - 1 - i) {
					sum2 += a[i][j];
				}
			}
		}
		System.out.println("主对角线之和为:" + sum1 + "  副对角线之和为:" + sum2);
	}

}
/*主对角线之和为:15.0  副对角线之和为:15.0
*/

5.将一个5×5的二维数组转置到另一个5×5的二维数组(即沿主对角线翻转)。

package work03;

public class Sum {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[][] a = { { 1, 1, 1, 1, 1 }, { 2, 2, 2, 2, 2 }, { 3, 3, 3, 3, 3 },
				{ 4, 4, 4, 4, 4 }, { 5, 5, 5, 5, 5 } };
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				System.out.print(a[i][j] + " ");
			}
			System.out.println();
		}
		System.out.println("转置后的矩阵:");
		zhuanzhi(a);

	}

	private static void zhuanzhi(int[][] a) {
		// TODO Auto-generated method stub
		/*
		 * int[][] b=new int[5][5]; 
		 * for(int i=0;i

6.编程找一个二维数组的鞍点,即该位置上的元素在该行上最大、在列上最小。

package work03;
import java.util.Scanner;  

public class Andian {  
  
    public static void main(String[] args) {  
        int a[][] = new int[111][111];  
        Scanner sc = new Scanner(System.in);  
        System.out.println("请输入行和列");
        int n = sc.nextInt();  
        int m = sc.nextInt();
        int i, j;  
        for (i = 0; i < n; i++) {  
            for (j = 0; j < m; j++) {  
                a[i][j] = sc.nextInt();  
            }  
        }  
        int max, maxj;  
        boolean flag = true;  
        for (i = 0; i < n; i++) {  
            max = a[i][0];  
            maxj = 0;  
            for (j = 0; j < m; j++) {  
                if (max < a[i][j]) {  
                    max = a[i][j];  
                    maxj = j;  
                }  
            }  
            flag = true;  
            for (int k = 0; k < n; k++) {  
                if (max > a[k][maxj]) {  
                    flag = false;  
                    continue;  
                }  
            }  
            if (flag) {  
                System.out.println("行号: " + i + " 列号: " + j + " 鞍点:" + max);  
                break;  
            }  
        }  
        if (flag == false) {  
            System.out.println("不存在鞍点");  
        }  
    }  
}  
/*
请输入行和列
2 3
1 2 3
4 5 6
行号: 0 列号: 3 鞍点:3
*/



你可能感兴趣的:(JAVA)