栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在一个对象中存储一个字符串和两个并行的arraylist。(java)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在一个对象中存储一个字符串和两个并行的arraylist。(java)

我建议您使用Map集合和枚举常量。
此外,测验类型和分数是紧密相关的数据,因此它们应该在单独的类别中。

为简单起见,请检查以下代码:

枚举类,用于存储测验的类型。

public enum QuizType {    SCIENCE, MATHS;}

QuizResult是一个POJO类,仅用于保存有关测验结果的数据。

public class QuizResult {    private QuizType quizType;    private double score;    public QuizResult(QuizType quizType, double score) {        this.quizType = quizType;        this.score = score;    }    public QuizType getQuizType() {        return quizType;    }    public void setQuizType(QuizType quizType) {        this.quizType = quizType;    }    public double getScore() {        return score;    }    public void setScore(double score) {        this.score = score;    }}

StudentQuiz类用于输入操作。评论应解释一切。

public class StudentQuiz {    public StudentQuiz() {        Map<String, List<QuizResult>> students = processStudentQuizes();        // Go through the keys of the map (which is the name of students)        for(String student : students.keySet()) { // Get all the quiz results of the student List<QuizResult> results = students.get(student); System.out.println(student + " "); for(QuizResult result : results) {     // Print the quiz type and the score     System.out.println(result.getQuizType().name() + " " + result.getScore()); }        }    }    private Map<String, List<QuizResult>> processStudentQuizes() {        Map<String, List<QuizResult>> students = new TreeMap<>();        System.out.println("Add student");        Scanner s = new Scanner(System.in);        String answer = null;        while(!"n".equalsIgnoreCase(answer)) { System.out.println("Student Name"); String studentName = s.next(); System.out.println("Quiz Name"); String quizName = s.next(); // Check if the quiz type actually exist boolean quizTypeOk = false; for(QuizType type : QuizType.values()) {     if(type.name().equalsIgnoreCase(quizName)) {         // We found the quiz type, so we can break from this loop         quizTypeOk = true;         break;     } } if(!quizTypeOk) {     // Quiz type couldn't be found, so print the message and start the while loop again     System.out.println("No such type of quiz! Please start again!");     continue; } System.out.println("Average Score"); double avgScore = s.nextDouble(); // Now we can create a QuizResult object from the inputed data QuizResult result = new QuizResult(QuizType.valueOf(quizName), avgScore); // Check if the student already exist if(students.containsKey(studentName)) {     // Add a new result to the list of results for the already existing student     students.get(studentName).add(result); } else {     // Create a new student in the map with a new results list and put the result into the list     List<QuizResult> results = new ArrayList<>();     results.add(result);     students.put(studentName, results); } System.out.println("Add another Student Y/N"); answer = s.next();        }        s.close();        return students;    }    public static void main(String[] args) {        new StudentQuiz();    }}

我希望这有帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516119.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号