功能要求:
搭建界面:
//在java中,我们可以用loop等字符来标记while循环,当想要结束循环时,可以用break loop 结束循环
loop:
while (true) {
System.out.println(" 请输入你的操作选项 ");
System.out.println("-------------------------------------");
System.out.println("-------1:添加学生信息------------------");
System.out.println("-------2:求平均成绩,以降序输出----------");
System.out.println("-------3:输出全组平均分,最高分和最低分----");
System.out.println("-------4:输入姓名,查询成绩--------------");
System.out.println("-------5:谢谢使用,再见-----------------");
System.out.println("-------------------------------------");
Scanner sc = new Scanner(System.in);
String choose = sc.next();
switch (choose) {
case "1": {
//添加学生信息
addStudent(list);
break;
}
case "2": {
//计算学生平均成绩以降序输出
averageScore(list);
break;
}
case "3": {
//计算各科目平均成绩,输出最高最低成绩
allaverageScore(list);
break;
}
case "4": {
//输入名字,查询该学生各科成绩
searchName(list);
break;
}
case "5": {
//退出循环,结束本次使用
System.out.println("谢谢使用,再见");
break loop;
}
default: {
System.out.println("输入错误,请重新输入");
}
}
}
}
默认初始化成员信息:实现在集合set集合里面初始化成员信息,利用静态代码块初始化成员信息
static TreeSet list=new TreeSet<>();
//利用静态代码块实现初始化成员列表
static{
list.add(new Student("2021032688","lmx",98,23,45,10));
}
函数1:添加学生信息
private static void addStudent(TreeSet list) {
System.out.println("请输入你要添加学生的个数");
Scanner sc=new Scanner(System.in);
int number=sc.nextInt();
for(int i=0;i
函数2:求每个学生的平均成绩,且以降序输出
private static void averageScore(TreeSet list) {
for (Student student : list) {
System.out.println(student);
}
}
//利用增强for实现对每一个对象各信息的输出
函数3:求各科目平均值,每个科目的最高分,最低分
private static void allaverageScore(TreeSet list) {
int n=list.size();
int sum=0;
int max=0;
int min=list.first().getApplied_Maths();
for (Student student : list) {
sum=sum+ student.getApplied_Maths();
if(student.getApplied_Maths()>max){
max= student.getApplied_Maths();
}
if(student.getApplied_Maths()max1){
max1= student.getCollege_English();
}
if(student.getCollege_English()max2){
max2= student.getComputerapplication_basics();
}
if(student.getComputerapplication_basics()max3){
max3= student.getApplied_Maths();
}
}
System.out.println("java的平均成绩="+sum3/n+" 该科目的最高成绩="+max3+" 该科目的最" + "该科目的最低成绩="+min3);
}
函数4:输入姓名,查询该学生的各科成绩
private static void searchName(TreeSet list) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入你要查询成绩的姓名");
String name=sc.next();
for (Student student : list) {
String name1=student.getName();
//为什么要在学生类里面重写equals方法,一个是字符串,一个重新创建的对象,所以要重写equals方法
if(name1.equals(name)){
System.out.println(student);
break;
}else{
System.out.println("该成绩库里面没有没有这个同学姓名");
}
}
}
学生类:
import java.util.Objects;
public class Student implements Comparable {
//私有化成员变量
private String ID;//学生学号
private String name;//学生姓名
private int Applied_Maths;//应用数学成绩
private int College_English;//大学英语成绩
private int Java_programming;//java成绩
private int Computerapplication_basics;//计算机成绩
//空参构造
public Student() {
}
//实参构造
public Student(String ID, String name, int applied_Maths, int college_English, int java_programming, int computerapplication_basics) {
this.ID = ID;
this.name = name;
Applied_Maths = applied_Maths;
College_English = college_English;
Java_programming = java_programming;
Computerapplication_basics = computerapplication_basics;
}
public String getID() {
return ID;
}
public Student setID(String ID) {
this.ID = ID;
return this;
}
public String getName() {
return name;
}
public Student setName(String name) {
this.name = name;
return this;
}
public int getApplied_Maths() {
return Applied_Maths;
}
public int setApplied_Maths(int applied_Maths) {
try {
if (applied_Maths >= 0 && applied_Maths <= 100) {
this.Applied_Maths = applied_Maths;
return Applied_Maths;
}
}catch(Exception e){
throw new RuntimeException();
}
return applied_Maths;
}
public int getCollege_English() {
return College_English;
}
public int setCollege_English(int college_English) {
try {
if (college_English >= 0 && college_English <= 100) {
this.College_English = college_English;
return College_English;
}
}catch(Exception e){
throw new RuntimeException();
}
return College_English;
}
public int getJava_programming() {
return Java_programming;
}
public int setJava_programming(int java_programming) {
try {
if (java_programming >= 0 && java_programming <= 100) {
this.Java_programming = java_programming;
return Java_programming;
}
}catch(Exception e){
throw new RuntimeException();
}
return Java_programming;
}
public int getComputerapplication_basics() {
return Computerapplication_basics;
}
public int setComputerapplication_basics(int computerapplication_basics) {
try {
if (computerapplication_basics >= 0 && computerapplication_basics <= 100) {
this.Computerapplication_basics = computerapplication_basics;
return Computerapplication_basics;
}
}catch(Exception e){
throw new RuntimeException();
}
return Computerapplication_basics;
}
//重写toString方法,可以通过调用对象直接打印对象的属性值
@Override
public String toString() {
return "Student{" +
"ID='" + ID + '\'' +
", name='" + name + '\'' +
", Applied_Maths=" + Applied_Maths +
", College_English=" + College_English +
", Java_programming=" + Java_programming +
", Computerapplication_basics=" + Computerapplication_basics +
" 平均成绩:="+((this.getApplied_Maths()+this.getJava_programming()+this.getCollege_English()+this.getComputerapplication_basics()))/4.0;
}
//实现Compareable接口,对其平均值进行降序输出
@Override
public int compareTo(Student o) {
int sum=this.getApplied_Maths()+this.getCollege_English()+this.getComputerapplication_basics()+this.getJava_programming();
int sum1=o.getApplied_Maths()+o.getCollege_English()+o.getComputerapplication_basics()+o.getJava_programming();
int i=sum1-sum;
return i == 0 ? this.getID().compareTo(o.getID()) : i;
}
//重写equals方法,实现输入的学生姓名与学生对象姓名进行匹配。
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Applied_Maths == student.Applied_Maths && College_English == student.College_English && Java_programming == student.Java_programming && Computerapplication_basics == student.Computerapplication_basics && ID.equals(student.ID) && Objects.equals(name, student.name);
}
}
测试类:
import java.util.Scanner;
import java.util.TreeSet;
public class App {
static TreeSet list=new TreeSet<>();
//利用静态代码块实现初始化成员列表
static{
list.add(new Student("2021032688","lmx",98,23,45,10));
}
public static void main(String[] args) {
loop:
while (true) {
System.out.println(" 请输入你的操作选项 ");
System.out.println("-------------------------------------");
System.out.println("-------1:添加学生信息------------------");
System.out.println("-------2:求平均成绩,以降序输出----------");
System.out.println("-------3:输出全组平均分,最高分和最低分----");
System.out.println("-------4:输入姓名,查询成绩--------------");
System.out.println("-------5:谢谢使用,再见-----------------");
System.out.println("-------------------------------------");
Scanner sc = new Scanner(System.in);
String choose = sc.next();
switch (choose) {
case "1": {
addStudent(list);
break;
}
case "2": {
averageScore(list);
break;
}
case "3": {
allaverageScore(list);
break;
}
case "4": {
searchName(list);
break;
}
case "5": {
System.out.println("谢谢使用,再见");
break loop;
}
default: {
System.out.println("输入错误,请重新输入");
}
}
}
}
private static void allaverageScore(TreeSet list) {
int n=list.size();
int sum=0;
int max=0;
int min=list.first().getApplied_Maths();
for (Student student : list) {
sum=sum+ student.getApplied_Maths();
if(student.getApplied_Maths()>max){
max= student.getApplied_Maths();
}
if(student.getApplied_Maths()max1){
max1= student.getCollege_English();
}
if(student.getCollege_English()max2){
max2= student.getComputerapplication_basics();
}
if(student.getComputerapplication_basics()max3){
max3= student.getApplied_Maths();
}
}
System.out.println("java的平均成绩="+sum3/n+" 该科目的最高成绩="+max3+" 该科目的最" + "该科目的最低成绩="+min3);
}
private static void searchName(TreeSet list) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入你要查询成绩的姓名");
String name=sc.next();
for (Student student : list) {
String name1=student.getName();
//为什么要在学生类里面重写equals方法,一个是字符串,一个重新创建的对象,所以要重写equals方法
if(name1.equals(name)){
System.out.println(student);
break;
}else{
System.out.println("该成绩库里面没有没有这个同学姓名");
}
}
}
private static void averageScore(TreeSet list) {
for (Student student : list) {
System.out.println(student);
}
}
private static void addStudent(TreeSet list) {
System.out.println("请输入你要添加学生的个数");
Scanner sc=new Scanner(System.in);
int number=sc.nextInt();
for(int i=0;i
运行结果测试:
主菜单:
添加学生信息:
计算学生平均成绩:
求出各科目的平均成绩,最高和最低成绩
输入姓名,查询成绩
退出程序
本次课程设计还有些不妥的地方,欢迎各位佬的批评指正。(java专栏到此就结束了,下次会更新Linuxshell相关的文章)