添加到也是HashMap

时间:2015-09-27 23:08:35

标签: java hashmap

import java.util.HashMap;

public class StudentDatabase {
private HashMap<String, int[]> quizmarks;

public static final int NUM_QUIZZES = 10;
public static final int MIN_GRADE = 0;
public static final int MAX_GRADE = 100;



public StudentDatabase(){
    quizmarks = new HashMap<String, int[]>();
}

public String formatName(String name){
    String caps = "";
    String lowercase = "";
    if(name != null && name.length() > 0){
        caps = name.substring(0,1).toUpperCase();
    }
    if(name.length() > 1){
        lowercase = name.substring(1).toLowerCase();
    }
    return caps + lowercase;
}


public void addStudent(String studentName){
    if(studentName != null){

        int[] marks = new int[NUM_QUIZZES];


        quizmarks.put(formatName(studentName), marks);
    }
}

public int[] getQuizzes(String student){
    if(student != null){
        System.out.println(quizmarks.get(student));

    }
    return null;
}

public void changeQuizMark
        (String studentName, int whichQuiz, int newMark){

    if(studentName!= null){
        quizmarks.get(studentName);
    }
    if(whichQuiz <= NUM_QUIZZES){
        quizmarks.get(whichQuiz);
    }

    if(newMark > MIN_GRADE && newMark < MAX_GRADE){

    }
    quizmarks.put(studentName, new int[]{newMark});
}

}

由于某些原因,Arrays没有完全教给我们,我们被鼓励上网获取帮助,所以我首先想弄清楚为什么它在addStudents方法中说它&# 34;无法将int转换为int []&#34;当我试图分配&#34;标记&#34;时,我非常确定双方都是int []数组。

我也不确定,因为我无法解决此错误,我的changeQuiz和newMarks字段实际上将如何分配给数组的右侧部分。每个学生基本上应该有十个问答。而我疯狂地陷入了困境。

我很抱歉我正在努力进行格式化,但这只是我第二次来到这里而且我完全忘记了如何格式化,它给了我一些重大问题,并且我试图按照说明最好我可以。

1 个答案:

答案 0 :(得分:0)

你的主要错误在于:

quizmarks.put(studentName, new int[]{newMark})

您不会在已存在的标记数组中添加新标记,但会创建一个仅包含新标记的新数组,因此会删除旧标记数组。您只需要获取所需的数组并按索引修改其元素。也做了一些小的改进:

import java.util.Arrays;
import java.util.HashMap;

public class StudentDatabase {
    private HashMap<String, int[]> quizmarks;

    public static final int NUM_QUIZZES = 10;
    public static final int MIN_GRADE = 0;
    public static final int MAX_GRADE = 100;


    public StudentDatabase() {
        quizmarks = new HashMap<>();
    }

    private String formatName(String name) {
        String caps = "";
        String lowercase = "";
        if (name != null && name.length() > 0) {
            caps = name.substring(0, 1).toUpperCase();
        }
        if (name.length() > 1) {
            lowercase = name.substring(1).toLowerCase();
        }
        return caps + lowercase;
    }


    public void addStudent(String studentName) {
        if (studentName != null) {
            int[] marks = new int[NUM_QUIZZES];
            quizmarks.put(formatName(studentName), marks);
        }
    }

    public void showQuizzes(String student) {
        System.out.println(Arrays.toString(quizmarks.get(student)));
    }

    public void changeQuizMark(String studentName, int whichQuiz, int newMark) {
        if (whichQuiz < NUM_QUIZZES && newMark >= MIN_GRADE && newMark =< MAX_GRADE) {
            quizmarks.get(studentName)[whichQuiz] = newMark;
        }
    }

}