用参数 数组方法编写代码,学员属性有姓名,学号,年龄,成绩,方法有添加学员信息,根据学员学号查找学员信息,展示所有学员信息(已录入的),根据查找学员姓名修改学员分数,对学员的成绩进行成绩的排序(排序后,)java
时间: 2024-01-11 20:01:57 浏览: 66
Java中使用数组完成学生成绩统计的多种实现代码清单.pdf
5星 · 资源好评率100%
以下是使用参数数组方法编写的代码:
```java
public class StudentManagement {
private String[] names;
private String[] ids;
private int[] ages;
private int[] scores;
private int count;
public StudentManagement(int capacity) {
names = new String[capacity];
ids = new String[capacity];
ages = new int[capacity];
scores = new int[capacity];
count = 0;
}
public void addStudent(String name, String id, int age, int score) {
names[count] = name;
ids[count] = id;
ages[count] = age;
scores[count] = score;
count++;
}
public void findStudentById(String id) {
for (int i = 0; i < count; i++) {
if (ids[i].equals(id)) {
System.out.println("姓名:" + names[i] + ",学号:" + ids[i] + ",年龄:" + ages[i] + ",成绩:" + scores[i]);
return;
}
}
System.out.println("找不到该学号的学生!");
}
public void showAllStudents() {
System.out.println("已录入的学生信息:");
for (int i = 0; i < count; i++) {
System.out.println("姓名:" + names[i] + ",学号:" + ids[i] + ",年龄:" + ages[i] + ",成绩:" + scores[i]);
}
}
public void updateScoreByName(String name, int score) {
for (int i = 0; i < count; i++) {
if (names[i].equals(name)) {
scores[i] = score;
System.out.println("已将学生 " + name + " 的成绩修改为 " + score + " 分!");
return;
}
}
System.out.println("找不到该姓名的学生!");
}
public void sortStudentsByScore() {
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (scores[j] < scores[j + 1]) {
int tempScore = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = tempScore;
String tempName = names[j];
names[j] = names[j + 1];
names[j + 1] = tempName;
String tempId = ids[j];
ids[j] = ids[j + 1];
ids[j + 1] = tempId;
int tempAge = ages[j];
ages[j] = ages[j + 1];
ages[j + 1] = tempAge;
}
}
}
System.out.println("按照成绩排序后的学生信息:");
for (int i = 0; i < count; i++) {
System.out.println("姓名:" + names[i] + ",学号:" + ids[i] + ",年龄:" + ages[i] + ",成绩:" + scores[i]);
}
}
}
```
其中,我们使用了一个名为 `count` 的变量来记录已经添加的学生数量,以便在查找、展示、修改和排序时不会遍历整个数组。在 `sortStudentsByScore()` 方法中,我们使用了冒泡排序的算法来按照成绩对学生进行排序。你可以在主函数中创建一个 `StudentManagement` 对象,然后调用其方法来完成对学生的管理。
阅读全文