3. 设计一个测试类StudentDemo,包含成员变量List<Student> students; 在main()方法中,调用并实现: (1)添加学生方法addStudents,实现从键盘输入学生信息,创建学生对象,添加到students 。 (2)删除学生方法delStudents,根据从键盘输入的学号信息,从students 删除该学生对象。 (3)输入成绩方法inputScore,从键盘为不同学生输入英语、数学和java成绩。 (4)输出成绩方法outScore, 输出students中所有学生的基本信息、3门课程成绩和平均成绩。
时间: 2023-05-27 07:04:08 浏览: 125
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentDemo {
private List<Student> students = new ArrayList<>();
/**
* 添加学生
*/
public void addStudents() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生信息:");
System.out.print("学号:");
String id = scanner.nextLine();
System.out.print("姓名:");
String name = scanner.nextLine();
System.out.print("性别:");
String gender = scanner.nextLine();
Student student = new Student(id, name, gender);
students.add(student);
System.out.println("添加成功!");
}
/**
* 删除学生
*/
public void delStudents() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生学号:");
String id = scanner.nextLine();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
students.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("该学生不存在!");
}
/**
* 输入成绩
*/
public void inputScore() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要输入成绩的学生学号:");
String id = scanner.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.print("请输入英语成绩:");
double englishScore = scanner.nextDouble();
System.out.print("请输入数学成绩:");
double mathScore = scanner.nextDouble();
System.out.print("请输入Java成绩:");
double javaScore = scanner.nextDouble();
student.setEnglishScore(englishScore);
student.setMathScore(mathScore);
student.setJavaScore(javaScore);
System.out.println("成绩输入成功!");
return;
}
}
System.out.println("该学生不存在!");
}
/**
* 输出成绩
*/
public void outScore() {
System.out.printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n", "学号", "姓名", "性别", "英语成绩", "数学成绩", "Java成绩", "平均成绩");
for (Student student : students) {
String id = student.getId();
String name = student.getName();
String gender = student.getGender();
double englishScore = student.getEnglishScore();
double mathScore = student.getMathScore();
double javaScore = student.getJavaScore();
double averageScore = (englishScore+mathScore+javaScore)/3;
System.out.printf("%-10s%-10s%-10s%-10.2f%-10.2f%-10.2f%-10.2f\n", id, name, gender, englishScore, mathScore, javaScore, averageScore);
}
}
public static void main(String[] args) {
StudentDemo demo = new StudentDemo();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1.添加学生 2.删除学生 3.输入成绩 4.输出成绩 5.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
demo.addStudents();
break;
case 2:
demo.delStudents();
break;
case 3:
demo.inputScore();
break;
case 4:
demo.outScore();
break;
case 5:
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
class Student {
private String id;
private String name;
private String gender;
private double englishScore;
private double mathScore;
private double javaScore;
public Student(String id, String name, String gender) {
this.id = id;
this.name = name;
this.gender = gender;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public double getJavaScore() {
return javaScore;
}
public void setJavaScore(double javaScore) {
this.javaScore = javaScore;
}
}
阅读全文