创建一个学生类型,包括姓名(字符串类型),性别(字符型),考试成绩(整数数组,长度为10)

作业:
1.创建一个学生类型,包括姓名(字符串类型),性别(字符型),考试成绩(整数数组,长度为10)
2.创建方法,计算平均分(去掉一个最高分,去掉一个最低分,剩余的取平均分)
3.编写一个方法,参数为学生数组,根据学生的平均成绩,按高到低排序。

 1 package lession0712;
 2 
 3 public class Student {
 4 
 5     private String name;
 6     private char sex;
 7     private int age;
 8     private int[] score = new int[10];
 9 
10     public String getName() {
11         return name;
12     }
13 
14     public void setName(String name) {
15         this.name = name;
16     }
17 
18     public char getSex() {
19         return sex;
20     }
21 
22     public void setSex(char sex) {
23         this.sex = sex;
24     }
25 
26     public int getAge() {
27         return age;
28     }
29 
30     public void setAge(int age) {
31         this.age = age;
32     }
33 
34     public int[] getScore() {
35         return score;
36     }
37 
38     public void setScore(int[] score) {
39         this.score = score;
40     }
41 
42     // 计算最大值
43     public int getMax() {
44         int max = score[0];
45         for (int i = 0; i < score.length; i++) {
46             if (score[i] > max) {
47                 max = score[i];
48             }
49         }
50         return max;
51     }
52 
53     // 计算最小值
54     public int getMin() {
55         int min = score[0];
56         for (int i = 0; i < score.length; i++) {
57             if (score[i] < min) {
58                 min = score[i];
59             }
60         }
61         return min;
62     }
63 
64     // 计算总分
65     public int getTotal() {
66         int sum = 0;
67         for (int i = 0; i < score.length; i++) {
68             sum += score[i];
69         }
70         return sum;
71     }
72 
73     // 计算平均分
74     public double avg() {
75         return 1.0 * (this.getTotal() - this.getMax() - this.getMin()) / (score.length - 2);
76     }
77 }

 

 1 package lession0712;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Zhu {
 6 
 7     public static void main(String[] args) {
 8 
 9         Scanner scan = new Scanner(System.in);
10         System.out.println("请输入学生个数(1-9):");
11         int n = scan.nextInt();
12 
13         Student[] stu = new Student[n];
14         for (int k = 0; k < n; k++) {
15             stu[k] = new Student();
16             stu[k].setName(f1(3));
17             char[] sex = { '男', '女' };
18             stu[k].setSex(sex[(int) (Math.random() * 2)]);
19             int[] score = new int[10];
20             for (int j = 0; j < 10; j++) {// 把第k个同学的成绩j随机从40-100赋值
21                 score[j] = (int) (Math.random() * 60 + 40);
22             }
23             stu[k].setScore(score);
24         }
25 
26         for (int i = 0; i < n; i++) {
27             System.out.println("第" + (i + 1) + "个学生:");
28             System.out.print("姓名:" + stu[i].getName() + " ");
29             System.out.print("姓别:" + stu[i].getSex() + " ");
30             System.out.print("成绩: ");
31             for (int j = 0; j < stu[i].getScore().length; j++) {
32                 System.out.print(stu[i].getScore()[j] + "  ");
33             }
34             System.out.println();
35         }
36         sort(stu);
37         scan.close();
38     }
39 
40     public static void sort(Student[] students) {
41         for (int i = 0; i < students.length - 1; i++) {
42             for (int j = i + 1; j < students.length; j++) {
43                 if (students[j].avg() > students[i].avg()) {
44                     Student t = students[i];
45                     students[i] = students[j];
46                     students[j] = t;
47                 }
48             }
49         }
50 
51         System.out.println();
52         System.out.println("根据学生的平均成绩,按高到低排序:");
53         for (int i = 0; i < students.length; i++) {
54             System.out.print("第" + (i + 1) + "名:" + students[i].getName() + " " + " 性别:" + students[i].getSex());
55             System.out.print(" 成绩:");
56             for (int j = 0; j < students[i].getScore().length; j++) {
57                 System.out.print(students[i].getScore()[j] + "  ");
58             }
59             System.out.println(" 平均分" + students[i].avg());
60         }
61 
62     }
63 
64     private static String f1(int t) {
65         String s = new String();
66         for (int j = 0; j < t; j++) {
67             char c = (char) (Math.random() * 25 + 97);
68             s += c;
69         }
70         return s;
71     }
72 }
View Code

输出结果:

 

 

posted @ 2022-07-13 19:14  扫码登录  阅读(131)  评论(0)    收藏  举报