编写一个Java应用程序。设计一个学生类Students,包括属性有:学号,姓名,性别,专业,三门课程成绩(计算机,数学,英语);包括方法有:求成绩总分,成绩平均分,各个属性的赋值方法,各个属性值的获取方法等等。
编写一个主类StudentsDemo.java,利用Students类输入5名同学的所有信息,按以下要求依次输出相关信息:
(1)输入一个学生的学号或者姓名,输出该学生的所有信息。
(2)数学课程(输入的第2门课程)最高分同学学号、姓名和课程分数
(3)总分最高与最低同学学号、姓名和课程分数
(4)有没有这样的学生存在:总分大于所有学生的平均成绩,但是至少有一门课不及格(低于60分)。若有,按行输出这些学生的全部信息;若没有,输出NOT EXIST。
分析:对于以上每个要求,编写相应的成员方法(函数)实现对应的功能,在测试类(StudentsDemo)中,调用这些方法实现相应功能。
输入
5行,5名学生信息
输出
5行,学生的相关信息
样例输入 Copy
001 aa male math 80 85 78
002 bb female math 78 78 80
003 cc female math 95 95 98
004 dd male cs 89 92 95
005 ee female cs 89 92 95
aa
样例输出 Copy
001,aa,male,math,80.0,85.0,78.0
003,cc,95.0,95.0,98.0
003,cc,95.0,95.0,98.0
002,bb,78.0,78.0,80.0
NOT EXIST
该回答引用GPTᴼᴾᴱᴺᴬᴵ
Students类:
public class Students {
private String number; // 学号
private String name; // 姓名
private String gender; // 性别
private String major; // 专业
private double computerScore; // 计算机成绩
private double mathScore; // 数学成绩
private double englishScore; // 英语成绩
// 构造方法
public Students(String number, String name, String gender, String major, double computerScore, double mathScore, double englishScore) {
this.number = number;
this.name = name;
this.gender = gender;
this.major = major;
this.computerScore = computerScore;
this.mathScore = mathScore;
this.englishScore = englishScore;
}
// 求成绩总分
public double getTotalScore() {
return computerScore + mathScore + englishScore;
}
// 求成绩平均分
public double getAverageScore() {
return getTotalScore() / 3;
}
// 各个属性的赋值方法
public void setNumber(String number) {
this.number = number;
}
public void setName(String name) {
this.name = name;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setMajor(String major) {
this.major = major;
}
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
}
// 各个属性值的获取方法
public String getNumber() {
return number;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public String getMajor() {
return major;
}
public double getComputerScore() {
return computerScore;
}
public double getMathScore() {
return mathScore;
}
public double getEnglishScore() {
return englishScore;
}
}
StudentsDemo类:
import java.util.Scanner;
public class StudentsDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Students[] students = new Students[5];
for (int i = 0; i < 5; i++) {
students[i] = new Students();
String line = scanner.nextLine();
String[] parts = line.split(" ");
students[i].setNo(parts[0]);
students[i].setName(parts[1]);
students[i].setGender(parts[2]);
students[i].setMajor(parts[3]);
students[i].setScore(1, Double.parseDouble(parts[4]));
students[i].setScore(2, Double.parseDouble(parts[5]));
students[i].setScore(3, Double.parseDouble(parts[6]));
}
String input = scanner.nextLine();
// 查找学号或姓名匹配的学生
for (int i = 0; i < 5; i++) {
if (students[i].getNo().equals(input) || students[i].getName().equals(input)) {
System.out.println(students[i].toString());
}
}
// 查找数学最高分的学生
int mathMaxIndex = 0;
for (int i = 1; i < 5; i++) {
if (students[i].getScore(2) > students[mathMaxIndex].getScore(2)) {
mathMaxIndex = i;
}
}
System.out.println(students[mathMaxIndex].getNo() + "," + students[mathMaxIndex].getName() + ","
+ students[mathMaxIndex].getScore(2));
// 查找总分最高和最低的学生
int totalMaxIndex = 0, totalMinIndex = 0;
for (int i = 1; i < 5; i++) {
if (students[i].getTotalScore() > students[totalMaxIndex].getTotalScore()) {
totalMaxIndex = i;
}
if (students[i].getTotalScore() < students[totalMinIndex].getTotalScore()) {
totalMinIndex = i;
}
}
System.out.println(students[totalMaxIndex].getNo() + "," + students[totalMaxIndex].getName() + ","
+ students[totalMaxIndex].getTotalScore());
System.out.println(students[totalMinIndex].getNo() + "," + students[totalMinIndex].getName() + ","
+ students[totalMinIndex].getTotalScore());
// 查找总分大于平均分,但有至少一门不及格的学生
double avgScore = 0;
for (int i = 0; i < 5; i++) {
avgScore += students[i].getTotalScore();
}
avgScore /= 5;
boolean exist = false;
for (int i = 0; i < 5; i++) {
if (students[i].getTotalScore() > avgScore) {
boolean hasFail = false;
for (int j = 1; j <= 3; j++) {
if (students[i].getScore(j) < 60) {
hasFail = true;
break;
}
}
if (hasFail) {
System.out.println(students[i].toString());
exist = true;
}
}
}
if (!exist) {
System.out.println("NOT EXIST");
}
}
}
import java.util.Scanner;
public class Students {
private String id;
private String name;
private String gender;
private String major;
private double[] scores;
// 构造函数
public Students(String id, String name, String gender, String major, double[] scores) {
this.id = id;
this.name = name;
this.gender = gender;
this.major = major;
this.scores = scores;
}
// 获取学号
public String getId() {
return id;
}
// 设置学号
public void setId(String id) {
this.id = id;
}
// 获取姓名
public String getName() {
return name;
}
// 设置姓名
public void setName(String name) {
this.name = name;
}
// 获取性别
public String getGender() {
return gender;
}
// 设置性别
public void setGender(String gender) {
this.gender = gender;
}
// 获取专业
public String getMajor() {
return major;
}
// 设置专业
public void setMajor(String major) {
this.major = major;
}
// 获取三门课程的成绩
public double[] getScores() {
return scores;
}
// 设置三门课程的成绩
public void setScores(double[] scores) {
this.scores = scores;
}
// 求成绩总分
public double getTotalScore() {
double total = 0;
for (double score : scores) {
total += score;
}
return total;
}
// 成绩平均分
public double getAverageScore() {
double total = getTotalScore();
return total / scores.length;
}
}
public class StudentsDemo {
private static final int NUM_STUDENTS = 5;
private static Students[] students = new Students[NUM_STUDENTS];
public static void main(String[] args) {
inputStudents();
Scanner scanner = new Scanner(System.in);
System.out.print("输入学生的学号或姓名:");
String idOrName = scanner.nextLine();
// 根据学号或姓名查询学生信息
findStudent(idOrName);
// 求数学课程最高分同学学号、姓名和课程分数
findMaxScore("数学");
// 求总分最高与最低同学学号、姓名和课程分数
findMaxAndMinScore();
// 判断总分大于所有学生的平均成绩,但是至少有一门课不及格(低于60分)的学生
findFailedStudents();
}
// 输入学生信息
private static void inputStudents() {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < NUM_STUDENTS; i++) {
System.out.println("请输入第 " + (i + 1) + " 个学生的信息:");
System.out.print("学号:");
String id = scanner.nextLine();
System.out.print("姓名:");
String name = scanner.nextLine();
System.out.print("性别:");
String gender = scanner.nextLine();
System.out.print("专业:");
String major = scanner.nextLine();
System.out.print("计算机成绩:");
package csdn20230301;
import java.math.BigDecimal;
import java.util.*;
/**
* @Description
* @Author wangFei
* @Date 2023/3/1 9:11
* @Version 1.0
**/
public class StudentsDemo {
static List<Students> list = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入");
for (int i = 0;i < 5;i++) {
String next = scanner.nextLine();
String[] s = next.split(" ");
List<String> list = Arrays.asList(s);
StudentsDemo.list.add(new Students(list.get(0),list.get(1),list.get(2),list.get(3),
new BigDecimal(list.get(4)),new BigDecimal(list.get(5)),new BigDecimal(list.get(6))));
}
Students students = new Students();
System.out.println("(1)答案:");
System.out.print("选择数字输入选择学号还是姓名:");
System.out.println("1、学号;2、姓名");
String id = null;
String name = null;
int i = scanner.nextInt();
if (i == 1) {
System.out.print("请输入学号:");
id = scanner.next();
}
if (i == 2) {
System.out.print("请输入姓名:");
name = scanner.next();
}
students.showStudentByIdOrName(id,name,list);
System.out.println("(2)答案:");
students.maxMath(list);
System.out.println("(3)答案:");
students.totalOfMaxOrMin(list);
System.out.println("(4)答案:");
students.isExist(list);
}
}
class Students {
public static void main(String[] args) {
String s = "1 aa male math 80 85 78";
String[] s1 = s.split(" ");
for (String ss : s1) {
System.out.println(ss);
}
}
private String id;
private String name;
private String sex;
private String personal;
private BigDecimal computerScore;
private BigDecimal mathScore;
private BigDecimal englishScore;
public Students() {}
public Students(String id, String name, String sex, String personal, BigDecimal computerScore, BigDecimal mathScore, BigDecimal englishScore) {
this.id = id;
this.name = name;
this.sex = sex;
this.personal = personal;
this.computerScore = computerScore;
this.mathScore = mathScore;
this.englishScore = englishScore;
}
/**
* 成绩总分
*/
public BigDecimal getScoreTotal(Students students) {
return students.getComputerScore()
.add(students.getMathScore()).add(students.getEnglishScore());
}
/**
* 平均分
*/
public void getScoreAvg(Students students) {
System.out.println("学生:" + students.getName() + "的平均分为:" +
getScoreTotal(students).divide(new BigDecimal("3")));
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPersonal() {
return personal;
}
public void setPersonal(String personal) {
this.personal = personal;
}
public BigDecimal getComputerScore() {
return computerScore;
}
public void setComputerScore(BigDecimal computerScore) {
this.computerScore = computerScore;
}
public BigDecimal getMathScore() {
return mathScore;
}
public void setMathScore(BigDecimal mathScore) {
this.mathScore = mathScore;
}
public BigDecimal getEnglishScore() {
return englishScore;
}
public void setEnglishScore(BigDecimal englishScore) {
this.englishScore = englishScore;
}
@Override
public String toString() {
return id +
", " + name +
", " + sex +
", " + personal +
", " + computerScore +
", " + mathScore +
", " + englishScore;
}
/**
* (1)输入一个学生的学号或者姓名,输出该学生的所有信息。
*/
public void showStudentByIdOrName(String id,String name,List<Students> list) {
if (id != null) {
if (list != null && list.size() > 0) {
for (Students students : list) {
String id1 = students.getId();
if (id.equals(id1)) {
System.out.println(students);
}
}
}else {
System.out.println("当前班级还没有学生,请添加!");
}
}
if (name != null && !"".equals(name)) {
if (list != null && list.size() > 0) {
for (Students students : list) {
String name1 = students.getName();
if (name.equals(name1)) {
System.out.println(students);
}
}
}else {
System.out.println("当前班级还没有学生,请添加!");
}
}
if (id != null && name != null && !"".equals(name)) {
if (list != null && list.size() > 0) {
for (Students students : list) {
String name1 = students.getName();
String id1 = students.getId();
if (name.equals(name1) && id.equals(id1)) {
System.out.println(students);
}else {
System.out.println("学号:" + id + ",名称:" + name +"的学生不存在");
}
}
}else {
System.out.println("当前班级还没有学生,请添加!");
}
}
}
/**
* (2)数学课程(输入的第2门课程)最高分同学学号、姓名和课程分数
*/
public void maxMath(List<Students> list) {
BigDecimal temp = new BigDecimal("0");
if (list != null && list.size() > 0) {
for (Students students : list) {
BigDecimal mathScore = students.getMathScore();
if (mathScore.compareTo(temp) > 0) {
temp = mathScore;
}
}
for (Students students : list) {
BigDecimal mathScore = students.getMathScore();
if (mathScore.compareTo(temp) == 0) {
System.out.println("数学课程最高分同学学号为:" + students.getId() +
",姓名为:" + students.getName() + ",课程分数为:[" +
"计算机:" + students.getComputerScore() + ",数学:" +
students.getMathScore() + ",英语:" + students.getEnglishScore() + "]");
}
}
}else {
System.out.println("班级没有学生,无法计算数学最高分");
}
}
/**
* (3)总分最高与最低同学学号、姓名和课程分数
*/
public void totalOfMaxOrMin(List<Students> list) {
List<BigDecimal> sortList = new ArrayList<>();
if (list != null && list.size() > 0) {
for (Students students : list) {
BigDecimal total = this.getScoreTotal(students);
sortList.add(total);
}
Collections.sort(sortList);
// 最高分
BigDecimal maxTotal = sortList.get(sortList.size() - 1);
BigDecimal minTotal = sortList.get(0);
for (Students students : list) {
BigDecimal scoreTotal = this.getScoreTotal(students);
if (scoreTotal.compareTo(maxTotal) == 0) {
System.out.println("总分最高的学生的学号为:" +students.getId() +
",姓名为:" + students.getName() + ",课程分数为:[" +
"计算机:" + students.getComputerScore() + ",数学:" +
students.getMathScore() + ",英语:" + students.getEnglishScore() + "]");
}
}
for (Students students : list) {
BigDecimal scoreTotal = this.getScoreTotal(students);
if (scoreTotal.compareTo(minTotal) == 0) {
System.out.println("总分最低的学生的学号为:" +students.getId() +
",姓名为:" + students.getName() + ",课程分数为:[" +
"计算机:" + students.getComputerScore() + ",数学:" +
students.getMathScore() + ",英语:" + students.getEnglishScore() + "]");
}
}
}else {
System.out.println("班级没有学生,无法计算总分高低");
}
}
/**
* (4)有没有这样的学生存在:总分大于所有学生的平均成绩,但是至少有一门课不及格(低于60分)。
* 若有,按行输出这些学生的全部信息;若没有,输出NOT EXIST。
*/
public void isExist(List<Students> list) {
boolean isEx = false;
BigDecimal to = new BigDecimal("0");
if (list != null && list.size() > 0) {
for (Students students : list) {
// 所有学生的总分
BigDecimal total = this.getScoreTotal(students);
//
to.add(total);
}
// 所有学生的平均成绩
BigDecimal avg = to.divide(new BigDecimal("5"));
for (Students students : list) {
BigDecimal scoreTotal = this.getScoreTotal(students);
BigDecimal computerScore = students.getComputerScore();
BigDecimal mathScore = students.getMathScore();
BigDecimal englishScore = students.getEnglishScore();
BigDecimal scoreIs60 = new BigDecimal("60");
if (scoreTotal.compareTo(avg) > 0
&& (computerScore.compareTo(scoreIs60) < 0
|| mathScore.compareTo(scoreIs60) < 0
|| englishScore.compareTo(scoreIs60) < 0)
) {
// 有这样的学生
System.out.println(students);
isEx = true;
}
}
}else {
System.out.println("班级没有学生,无法计算存不存在!");
}
if (!isEx) {
System.out.println("NOT EXIST");
}
}
}
运行:
输入
001 aa male math 80 85 78
002 bb female math 78 78 80
003 cc female math 95 95 98
004 dd male cs 89 92 95
005 ee female cs 89 92 95
(1)答案:
选择数字输入选择学号还是姓名:1、学号;2、姓名
1
请输入学号:001
001, aa, male, math, 80, 85, 78
(2)答案:
数学课程最高分同学学号为:003,姓名为:cc,课程分数为:[计算机:95,数学:95,英语:98]
(3)答案:
总分最高的学生的学号为:003,姓名为:cc,课程分数为:[计算机:95,数学:95,英语:98]
总分最低的学生的学号为:002,姓名为:bb,课程分数为:[计算机:78,数学:78,英语:80]
(4)答案:
NOT EXIST