、编程题 定义一个表示学生信息的类Student,要求如下: (1)类Student的成员变量: sNO 表示学号;sName表示姓名;sSex表示性别;sAge表示年龄;sJava:表示Java课程成绩。 (2)类Student带参数的构造方法: 在构造方法中通过形参完成对成员变量的赋值操作。 (3)类Student的方法成员: getNo():获得学号; getName():获得姓名; getSex():获得性别; getAge()获得年龄; getJava():获得Java 课程成绩 根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。
时间: 2024-01-23 09:02:55 浏览: 92
设计一个学生类Student(学生学号、姓名、数学、英语、计算机成绩;)
5星 · 资源好评率100%
以下是Java代码实现:
```
public class Student {
private String sNO;
private String sName;
private String sSex;
private int sAge;
private double sJava;
// 构造方法
public Student(String no, String name, String sex, int age, double java) {
this.sNO = no;
this.sName = name;
this.sSex = sex;
this.sAge = age;
this.sJava = java;
}
// 获取学号
public String getNo() {
return sNO;
}
// 获取姓名
public String getName() {
return sName;
}
// 获取性别
public String getSex() {
return sSex;
}
// 获取年龄
public int getAge() {
return sAge;
}
// 获取Java成绩
public double getJava() {
return sJava;
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("001", "张三", "男", 18, 89.5);
Student s2 = new Student("002", "李四", "女", 19, 92.0);
Student s3 = new Student("003", "王五", "男", 20, 85.0);
Student s4 = new Student("004", "赵六", "女", 18, 78.5);
Student s5 = new Student("005", "孙七", "男", 19, 90.0);
// 输出每个学生的信息
System.out.println(s1.getNo() + " " + s1.getName() + " " + s1.getSex() + " " + s1.getAge() + " " + s1.getJava());
System.out.println(s2.getNo() + " " + s2.getName() + " " + s2.getSex() + " " + s2.getAge() + " " + s2.getJava());
System.out.println(s3.getNo() + " " + s3.getName() + " " + s3.getSex() + " " + s3.getAge() + " " + s3.getJava());
System.out.println(s4.getNo() + " " + s4.getName() + " " + s4.getSex() + " " + s4.getAge() + " " + s4.getJava());
System.out.println(s5.getNo() + " " + s5.getName() + " " + s5.getSex() + " " + s5.getAge() + " " + s5.getJava());
// 计算Java成绩的平均值、最大值和最小值
double avg = (s1.getJava() + s2.getJava() + s3.getJava() + s4.getJava() + s5.getJava()) / 5;
double max = Math.max(s1.getJava(), Math.max(s2.getJava(), Math.max(s3.getJava(), Math.max(s4.getJava(), s5.getJava()))));
double min = Math.min(s1.getJava(), Math.min(s2.getJava(), Math.min(s3.getJava(), Math.min(s4.getJava(), s5.getJava()))));
System.out.println("平均值:" + avg);
System.out.println("最大值:" + max);
System.out.println("最小值:" + min);
}
}
```
阅读全文