1)定义一个表示学生信息的类Student,要求如下: ① 类Student的成员变量: no 表示学号; name 表示姓名; sex 表示性别; score 表示Java课程成绩; ② 类Student的方法成员: getNo():获得学号; getName():获得姓名; getSex():获得性别; getAge()获得年龄; getScore():获得Java 课程成绩 getDescription(2):获得对学生信息进行描述说明的字符串创建第 (1)题的Student类的10个对象,找出java成绩最好的同学和成绩最低的同学。
时间: 2023-12-29 17:04:21 浏览: 100
设计学生类Student,属性:编号(整型);姓名(字符串),成绩(整型)。
5星 · 资源好评率100%
以下是实现题目要求的代码:
```java
public class Student {
private String no; // 学号
private String name; // 姓名
private String sex; // 性别
private int score; // Java课程成绩
// 构造方法
public Student(String no, String name, String sex, int score) {
this.no = no;
this.name = name;
this.sex = sex;
this.score = score;
}
// getter方法
public String getNo() {
return no;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public int getScore() {
return score;
}
// 获取对学生信息进行描述说明的字符串
public String getDescription() {
return "学号:" + no + ",姓名:" + name + ",性别:" + sex + ",Java课程成绩:" + score;
}
}
```
接下来,我们创建10个Student对象,并找出Java成绩最好和最差的同学。
```java
public class Main {
public static void main(String[] args) {
Student s1 = new Student("001", "张三", "男", 90);
Student s2 = new Student("002", "李四", "女", 85);
Student s3 = new Student("003", "王五", "男", 95);
Student s4 = new Student("004", "赵六", "女", 70);
Student s5 = new Student("005", "钱七", "男", 80);
Student s6 = new Student("006", "孙八", "女", 88);
Student s7 = new Student("007", "周九", "男", 92);
Student s8 = new Student("008", "吴十", "女", 65);
Student s9 = new Student("009", "郑一", "男", 87);
Student s10 = new Student("010", "王二", "女", 75);
// 找出Java成绩最好的同学
Student best = s1;
for (Student s : new Student[]{s2, s3, s4, s5, s6, s7, s8, s9, s10}) {
if (s.getScore() > best.getScore()) {
best = s;
}
}
System.out.println("Java成绩最好的同学:" + best.getDescription());
// 找出Java成绩最低的同学
Student worst = s1;
for (Student s : new Student[]{s2, s3, s4, s5, s6, s7, s8, s9, s10}) {
if (s.getScore() < worst.getScore()) {
worst = s;
}
}
System.out.println("Java成绩最低的同学:" + worst.getDescription());
}
}
```
输出结果为:
```
Java成绩最好的同学:学号:003,姓名:王五,性别:男,Java课程成绩:95
Java成绩最低的同学:学号:008,姓名:吴十,性别:女,Java课程成绩:65
```
阅读全文