代码共三部分
第一部分
package student.score.manage;
import java.util.Scanner;
public class ScoreList {
int maxLength = 100;
int length = 0;
Score[] data = new Score[maxLength];
Scanner input = new Scanner(System.in);
public ScoreList() {
}
//添加记录方法
public void add() {
if(length < maxLength) {
data[length] = new Score();
System.out.print("请输入学生的学号: ");
data[length].setNum(input.next());
System.out.print("请输入学生的姓名:");
data[length].setName(input.next());
System.out.print("请输入学生的算数成绩:");
data[length].setMath(input.nextDouble());
System.out.print("请输入学生的语文成绩:");
data[length].setChinese(input.nextDouble());
length++;
System.out.println("添加成功!");
}
else {
System.out.println("添加失败!");
}
}
//显示成绩单方法
public void transcript() {
System.out.println("=====================================");
System.out.println(" 学号 姓名 算数 语文 ");
for(int i = 0; i < length; i++) {
System.out.println(data[i].num + "\t" + data[i].name + "\t" + "\t" + data[i].math + "\t" + "\t" + data[i].chinese);
}
System.out.println("=====================================\n");
}
//成绩从高往低排序方法
public void sortmath() {
Score temp = new Score();
for(int i = 1; i < length; i++) {
for(int j = 0; j < length - i; j++) {
if(data[j].math < data[j + 1].math) { //冒泡法排序
temp = data[j]; data[j] = data[j + 1]; data[j + 1] = temp;
}
}
}
}
public void sortchinese() {
Score temp = new Score();
for(int i = 1; i < length; i++) {
for(int j = 0; j < length - i; j++) {
if(data[j].chinese < data[j + 1].chinese) { //冒泡法排序
temp = data[j]; data[j] = data[j + 1]; data[j + 1] = temp;
}
}
}
}
//平均分、最大最小值方法
public void average() {
double sum = 0.0;
double max = 0.0;
double min = 100.0;
for(int i = 0; i < length; i++) {
sum += data[i].math;
max = max > data[i].math ? max : data[i].math;
min = min < data[i].math ? min : data[i].math; //三目运算法
}
System.out.printf("算数课的平均成绩为: %.2f\n", sum/length);
System.out.println("最高分为: " + max + "\n" + "最低分为: " + min + "\n");
}
public void averagec() {
double sum = 0.0;
double max = 0.0;
double min = 100.0;
for(int i = 0; i < length; i++) {
sum += data[i].chinese;
max = max > data[i].chinese ? max : data[i].chinese;
min = min < data[i].chinese ? min : data[i].chinese; //三目运算法
}
System.out.printf("语文课的平均成绩为: %.2f\n", sum/length);
System.out.println("最高分为: " + max + "\n" + "最低分为: " + min + "\n");
}
//删除学生记录方法
public void delete() {
System.out.print("请输入您要删除信息的学生的学号: ");
String number1 = input.next();
int i, flag = length;
for(i = 0; i < length; i++) {
if(number1.equals(data[i].num)) {
flag = i; break;
}
}
if(i == length) {
System.out.println("查无此人!请核对后重新输入 \n");
delete();
}
else {
for(int j = flag; j < length; j++) {
data[j] = data[j + 1];
}
System.out.println("删除成功!\n");
length -= 1; //不减1会报数组越界的错误
}
}
//查询某个学生信息方法
public void inquire() {
System.out.print("请输入您要查询成绩的学生的学号:");
String number2 = input.next();
int i;
for(i = 0; i < length; i++) {
if(number2.equals(data[i].num)) {
System.out.println("====================================");
System.out.println(" 学号 姓名 算数成绩 语文成绩 ");
System.out.println(data[i].num + "\t" + data[i].name + "\t" + "\t" + data[i].math+ "\t" + "\t" + data[i].chinese);
System.out.println("====================================\n");
break;
}
}
if(i == length) {
System.out.println("查无此人!请核对后重新输入学号 \n");
inquire();
}
}
//修改学生信息方法
public void recompose() {
System.out.pr