请问这个Java该怎么做呀

 

创建一个学生实体类,一个成绩实体类。实例化一下,通过构造方法给实体类赋值。然后通过hashmap的put方法把实体类塞进去。

package com.xm.officialaccounts.entity;

import lombok.Builder;
import lombok.Data;

/**
 * @author 
 * @date created in 15:40 2021/5/28
 */
@Data
public class Student {
    private String id;
    private String name;
    private String className;

    @Builder
    private Student(String id, String name, String className) {
        this.id = id;
        this.name = name;
        this.className = className;
    }

    @Override
    public String toString(){
        return "学号:"+id+"\n姓名:"+name+"\n班级:"+className;
    }
}
package com.xm.officialaccounts.entity;

import lombok.Builder;
import lombok.Data;

/**
 * @author
 * @date created in 15:42 2021/5/28
 */
@Data
public class Grades {
    private Integer math;
    private Integer chinese;
    private Integer english;

    @Builder
    private Grades(Integer math, Integer chinese, Integer english) {
        this.math = math;
        this.chinese = chinese;
        this.english = english;
    }

    @Override
    public String toString(){
        return "数学:"+math+"\n语文:"+chinese+"\n英语:"+english;
    }
}
package com.xm.officialaccounts.entity;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author
 * @date created in 15:45 2021/5/28
 */
public class testStudent {
    static Map<Student,Grades> map = new HashMap<>(2);
    static {
        // 装数据
        map.put(Student.builder().id("2020011101").name("张三").className("3年1班").build(),Grades.builder()
                .chinese(82).english(85).math(88).build());
    }
    /**
     * 获取数据
     * @param name
     * @date 2021/5/28 15:50
     * @return void
     **/
    public static void getInfo(String name){
        map.keySet().forEach(e ->{
            if(e.getName().equals(name)){
                System.out.println(e.toString());
                System.out.println(map.get(e).toString());
            }
        });
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("姓名:");
        String name = sc.next();
        getInfo(name);
    }
}

改一下包名,我一定是太无聊了,才会搞这种小白的问题

第二个问

package com.xm.officialaccounts.entity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author 
 * @date created in 16:06 2021/5/28
 */
public class readStudentFile {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(new File("文件路径")));
        String line;
        Student student;
        ArrayList<Student> studentList = new ArrayList<>(16);
        while ((line = br.readLine()) != null){
            String[] stu = line.split("\t");
            student = Student.builder().id(stu[0]).name(stu[1]).className(stu[2]).build();
            studentList.add(student);
        }
        studentList.forEach(System.out::println);
    }
}

 

如果对你有帮助 ,请帮忙点个采纳。你的认可是我们互助的动力。