定义StudentScore类:
StudentScore类表示学生的成绩数据,一条学生成绩数据对应一个StudentScore类的实例对象,该类的要求如下:
两个私有的成员变量,stuID表成绩数据中的学号信息,score表示该学号对应的成绩分数;
一个带参数的构造器,用于初始化两个成员变量,按参数值实例化StudentScore的对象;
两个成员变量的setter和getter方法。
class StudentScore{
//按上述要求完善StudentScore类,并提交该类的代码
}
定义StudentScoreTable类:
StudentScoreTable类表示学生成绩单,该成绩单中有任意多条成绩数据(StudentScore),StudentScoreTable类的要求如下:
一个ArrayList类型的成员变量,该变量表示学生成绩数据列表,即是一个成绩数据的集合;
一个无参数的构造器,在构造器中初始化上述的成员变量;
一个成员方法:public void add(StudentScore score) 实现向成绩列表集合中添加一条成绩数据;
一个成员方法:public StudentScore findByStuID(String stuID) 实现根据学号从成绩列表集合中查询一条成绩数据,如果能找到,返回该成绩数据对象,反之返回null
class StudentScoreTable{
//按要求完善该类代码
}
Main类代码:
public class Main {
public static void main(String[] args) {
//实例化一个StudentScoreTable对象
StudentScoreTable table = new StudentScoreTable();
//从键盘输入成绩数据,格式为:stuID#分数
//一条数据一行,直到输入:!end!表示输入结束
Scanner sc = new Scanner(System.in);
while (true){
String input = sc.nextLine();
if (input.equals("!end!")){
break;
}
//拆分输入的成绩数据
String[] inputs = input.split("#");
if (inputs.length != 2){
break;
}
//将拆分的数据生成StudentScore对象,并加入到成绩单的成绩列表中
StudentScore score = new StudentScore(inputs[0],Double.parseDouble(inputs[1]));
table.add(score);
}
//再输入一个学号,按该学号从成绩单列表中查询对应的分数,并输出查询结果
String stuID = sc.next();
StudentScore queryScore = table.findByStuID(stuID);
if (queryScore == null){
System.out.println("未找到对应的成绩数据");
}else{
System.out.println("学号:"+stuID+"的分数为:"+queryScore.getScore());
}
}
}
输入样例:
在这里给出一组输入。例如:
2020121001#78.2
2020121002#89.2
2020121003#89.5
2020121004#90.3
!end!
2020121002
相应输出:
学号:2020121002的分数为:89.2
代码有问题吗?