用java设计一个用于统计一个班学生成绩的程序,需要完成以下功能: 1)输入学生成绩信息 2)按总平均成绩排序 3)按学号排序 4)输出学生成绩表 5)结束退出 根据以上描述,编写程序输出功能菜单,输入菜单选择,根据选择执行对应功能,每个功能只显示该功能名称,例如选2,则输出:“现在执行按总平均成绩排序功能!”
时间: 2024-02-18 11:06:10 浏览: 17
好的,以下是Java代码实现:
```java
import java.util.*;
public class Main {
static Scanner scanner = new Scanner(System.in);
static List<Student> students = new ArrayList<>();
public static void main(String[] args) {
while (true) {
System.out.println("请选择功能:");
System.out.println("1. 输入学生成绩信息");
System.out.println("2. 按总平均成绩排序");
System.out.println("3. 按学号排序");
System.out.println("4. 输出学生成绩表");
System.out.println("5. 结束退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
inputStudents();
break;
case 2:
sortByAverageScore();
break;
case 3:
sortById();
break;
case 4:
printStudents();
break;
case 5:
System.exit(0);
default:
System.out.println("输入不正确,请重新输入!");
break;
}
}
}
private static void inputStudents() {
System.out.println("请输入学生数:");
int count = scanner.nextInt();
for (int i = 0; i < count; i++) {
System.out.println("请输入第" + (i + 1) + "个学生的信息:");
System.out.println("学号:");
int id = scanner.nextInt();
System.out.println("姓名:");
String name = scanner.next();
System.out.println("语文成绩:");
int chineseScore = scanner.nextInt();
System.out.println("数学成绩:");
int mathScore = scanner.nextInt();
System.out.println("英语成绩:");
int englishScore = scanner.nextInt();
Student student = new Student(id, name, chineseScore, mathScore, englishScore);
students.add(student);
}
}
private static void sortByAverageScore() {
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return Double.compare(o2.getAverageScore(), o1.getAverageScore());
}
});
System.out.println("现在执行按总平均成绩排序功能!");
}
private static void sortById() {
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return Integer.compare(o1.getId(), o2.getId());
}
});
System.out.println("现在执行按学号排序功能!");
}
private static void printStudents() {
if (students.size() == 0) {
System.out.println("还没有输入学生信息!");
return;
}
System.out.println("学号\t姓名\t语文\t数学\t英语\t总分\t平均分");
for (Student student : students) {
System.out.println(student);
}
}
}
class Student {
private int id;
private String name;
private int chineseScore;
private int mathScore;
private int englishScore;
public Student(int id, String name, int chineseScore, int mathScore, int englishScore) {
this.id = id;
this.name = name;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.englishScore = englishScore;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getChineseScore() {
return chineseScore;
}
public int getMathScore() {
return mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public int getTotalScore() {
return chineseScore + mathScore + englishScore;
}
public double getAverageScore() {
return getTotalScore() / 3.0;
}
@Override
public String toString() {
return id + "\t" + name + "\t" + chineseScore + "\t" + mathScore + "\t" + englishScore
+ "\t" + getTotalScore() + "\t" + String.format("%.2f", getAverageScore());
}
}
```
你可以将以上代码保存为 `Main.java` 文件,并在命令行中编译并运行。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)