用java定义一个表示学生信息的类Student,要求如下: (1)类Student的成员变量: sNO 表示学号;sName表示姓名;sSex表示性别;sAge表示年龄;sJava:表示Java课程成绩。 (2)类Student带参数的构造方法: 在构造方法中通过形参完成对成员变量的赋值操作。 (3)类Student的方法成员: getNo():获得学号; getName():获得姓名; getSex():获得性别; getAge()获得年龄; getJava():获得Java 课程成绩 根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。
时间: 2024-01-19 13:04:05 浏览: 79
以下是实现上述要求的Java代码:
```java
public class Student {
private String sNO;
private String sName;
private String sSex;
private int sAge;
private double sJava;
public Student(String sNO, String sName, String sSex, int sAge, double sJava) {
this.sNO = sNO;
this.sName = sName;
this.sSex = sSex;
this.sAge = sAge;
this.sJava = sJava;
}
public String getNo() {
return sNO;
}
public String getName() {
return sName;
}
public String getSex() {
return sSex;
}
public int getAge() {
return sAge;
}
public double getJava() {
return sJava;
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("001", "张三", "男", 20, 80.0);
Student s2 = new Student("002", "李四", "女", 21, 85.5);
Student s3 = new Student("003", "王五", "男", 19, 75.5);
Student s4 = new Student("004", "赵六", "女", 22, 90.0);
Student s5 = new Student("005", "刘七", "男", 20, 78.5);
System.out.println("学生信息:");
System.out.println(s1.getNo() + "\t" + s1.getName() + "\t" + s1.getSex() + "\t" + s1.getAge() + "\t" + s1.getJava());
System.out.println(s2.getNo() + "\t" + s2.getName() + "\t" + s2.getSex() + "\t" + s2.getAge() + "\t" + s2.getJava());
System.out.println(s3.getNo() + "\t" + s3.getName() + "\t" + s3.getSex() + "\t" + s3.getAge() + "\t" + s3.getJava());
System.out.println(s4.getNo() + "\t" + s4.getName() + "\t" + s4.getSex() + "\t" + s4.getAge() + "\t" + s4.getJava());
System.out.println(s5.getNo() + "\t" + s5.getName() + "\t" + s5.getSex() + "\t" + s5.getAge() + "\t" + s5.getJava());
double sum = s1.getJava() + s2.getJava() + s3.getJava() + s4.getJava() + s5.getJava();
double avg = sum / 5.0;
System.out.println("平均成绩:" + avg);
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("最大成绩:" + max);
System.out.println("最小成绩:" + min);
}
}
```
输出结果:
```
学生信息:
001 张三 男 20 80.0
002 李四 女 21 85.5
003 王五 男 19 75.5
004 赵六 女 22 90.0
005 刘七 男 20 78.5
平均成绩:81.5
最大成绩:90.0
最小成绩:75.5
```
阅读全文