Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从HashMaps计算平均分数_Java_Eclipse - Fatal编程技术网

Java 从HashMaps计算平均分数

Java 从HashMaps计算平均分数,java,eclipse,Java,Eclipse,我不知道该如何将其转化为代码。我正在构建一个小代码块,其中通过构建一个新的hashmap,并将密钥和新的数字放在那里,给出两个具有相同密钥的hashmap中的两个数字的商。e、 HashMap1有{a=6},HashMap2有{a=3}。然后HashMap3将放置{a=2}。 这就是我正在研究的代码 /** * Given a list of reviews from the file, this method computes the average score *

我不知道该如何将其转化为代码。我正在构建一个小代码块,其中通过构建一个新的hashmap,并将密钥和新的数字放在那里,给出两个具有相同密钥的hashmap中的两个数字的商。e、 HashMap1有{a=6},HashMap2有{a=3}。然后HashMap3将放置{a=2}。 这就是我正在研究的代码

    /**
     * Given a list of reviews from the file, this method computes the average score
     * for each word in the reviews and stores that score in the wordValue HashMap,
     * where the key is the word and the value is the average score.
     * 
     * To get the average score, first compute the total score for a word and the
     * number of times it appears.
     * 
     * As a slight improvement, only store the word in wordValue if the score is not
     * an average word - if the score is less than 1.75 or greater than 2.25.
     * 
     * @param reviews An ArrayList of lines representing reviews. The first word in
     *                each line is a score.
     */
    public void computeWordValue(ArrayList<String> reviews) {

        // Initialize any needed HashMaps
        HashMap<String, Integer> totalScores = new HashMap<>();
        HashMap<String, Integer> wordCount = new HashMap<>();
        // Compute the word total scores and counts using the appropriate method.
        computeScoreAndCounts(reviews, totalScores, wordCount);
        // Build a HashMap of average scores
        HashMap<String, Double> avgScoresMap = new HashMap<>();//I'm assuming this is empty

        for (String wordKey : reviews) {
            
            if (avgScoresMap.containsKey(wordKey)) {
                Double avgScore = (double) (totalScores.get(wordKey) / wordCount.get(wordKey)); //I think this is why it doesn't work
                avgScoresMap.put(wordKey, avgScore);
            }

        }
        System.out.println(avgScoresMap);
    }
/**
*给定文件中的评论列表,此方法计算平均分数
*对于评论中的每个单词,并将分数存储在wordValue HashMap中,
*其中,关键字是单词,值是平均分数。
* 
*要获得平均分数,首先计算一个单词的总分和
*它出现的次数。
* 
*作为一个小小的改进,如果分数不正确,只将单词存储在wordValue中
*平均值-如果分数小于1.75或大于2.25。
* 
*@param审阅表示审阅的行的数组列表。第一个字
*每一行都是分数。
*/
public void computeWordValue(ArrayList审查){
//初始化任何需要的哈希映射
HashMap totalScores=新HashMap();
HashMap wordCount=新HashMap();
//使用适当的方法计算单词总分和计数。
计算CoreandCounts(评论、总分、字数);
//构建平均分数的哈希图
HashMap avgScoresMap=newhashmap();//我假设它是空的
for(字符串wordKey:reviews){
if(avgScoresMap.containsKey(wordKey)){
Double avgScore=(Double)(totalScores.get(wordKey)/wordCount.get(wordKey));//我想这就是它不起作用的原因
avgScoresMap.put(wordKey,avgScore);
}
}
系统输出打印LN(avgScoresMap);
}
我不知道为什么这不起作用,它只打印出什么或“{}”。不要担心前两个hashmap。 谢谢,这是你的问题

HashMap<String, Double> avgScoresMap = new HashMap<>();//I'm assuming this is empty
for (String wordKey : reviews){
    if (avgScoresMap.containsKey(wordKey)) {
            Double avgScore = (double) (totalScores.get(wordKey)
                    / wordCount.get(wordKey)); // I think this is why it doesn't work
            avgScoresMap.put(wordKey, avgScore);
    }
}
HashMap avgScoresMap=newhashmap()//我想这是空的
for(字符串wordKey:reviews){
if(avgScoresMap.containsKey(wordKey)){
Double avgScore=(Double)(totalScores.get(wordKey)
/wordCount.get(wordKey));//我想这就是它不起作用的原因
avgScoresMap.put(wordKey,avgScore);
}
}
正如您所说,hashmap是空的。因此它不能包含任何内容,因此您的
containsKey
失败。因此,你从来没有把任何东西放在地图上。更改它,以便它检查它是否不在地图中。

这是您的问题

HashMap<String, Double> avgScoresMap = new HashMap<>();//I'm assuming this is empty
for (String wordKey : reviews){
    if (avgScoresMap.containsKey(wordKey)) {
            Double avgScore = (double) (totalScores.get(wordKey)
                    / wordCount.get(wordKey)); // I think this is why it doesn't work
            avgScoresMap.put(wordKey, avgScore);
    }
}
HashMap avgScoresMap=newhashmap()//我想这是空的
for(字符串wordKey:reviews){
if(avgScoresMap.containsKey(wordKey)){
Double avgScore=(Double)(totalScores.get(wordKey)
/wordCount.get(wordKey));//我想这就是它不起作用的原因
avgScoresMap.put(wordKey,avgScore);
}
}
正如您所说,hashmap是空的。因此它不能包含任何内容,因此您的
containsKey
失败。因此,你从来没有把任何东西放在地图上。更改它,以便它检查它是否不在地图中。

这是您的代码:

 for (String wordKey : reviews) {
            
            if (avgScoresMap.containsKey(wordKey)) {
                Double avgScore = (double) (totalScores.get(wordKey) / wordCount.get(wordKey)); //I think this is why it doesn't work
                avgScoresMap.put(wordKey, avgScore);
            }

        }
但是,它不是
if(avgScoresMap.containsKey(wordKey))
,它应该是
if(!avgScoresMap.containsKey(wordKey))
,因为您正在检查映射是否包含密钥。如果不这样做,它就永远不会将数据放入
HashMap
。或者换句话说,这应该是您的方法:

public void computeWordValue(ArrayList<String> reviews) {

        // Initialize any needed HashMaps
        HashMap<String, Integer> totalScores = new HashMap<>();
        HashMap<String, Integer> wordCount = new HashMap<>();
        // Compute the word total scores and counts using the appropriate method.
        computeScoreAndCounts(reviews, totalScores, wordCount);
        // Build a HashMap of average scores
        HashMap<String, Double> avgScoresMap = new HashMap<>();//I'm assuming this is empty

        for (String wordKey : reviews) {
            
            if (!avgScoresMap.containsKey(wordKey)) {
                double avgScore = (double) (totalScores.get(wordKey) / wordCount.get(wordKey)); //I think this is why it doesn't work
                avgScoresMap.put(wordKey, avgScore);
            }

        }
        System.out.println(avgScoresMap);
    }
public void computeWordValue(ArrayList审查){
//初始化任何需要的哈希映射
HashMap totalScores=新HashMap();
HashMap wordCount=新HashMap();
//使用适当的方法计算单词总分和计数。
计算CoreandCounts(评论、总分、字数);
//构建平均分数的哈希图
HashMap avgScoresMap=newhashmap();//我假设它是空的
for(字符串wordKey:reviews){
如果(!avgScoresMap.containsKey(wordKey)){
double avgScore=(double)(totalScores.get(wordKey)/wordCount.get(wordKey));//我想这就是它不起作用的原因
avgScoresMap.put(wordKey,avgScore);
}
}
系统输出打印LN(avgScoresMap);
}
这是您的代码:

 for (String wordKey : reviews) {
            
            if (avgScoresMap.containsKey(wordKey)) {
                Double avgScore = (double) (totalScores.get(wordKey) / wordCount.get(wordKey)); //I think this is why it doesn't work
                avgScoresMap.put(wordKey, avgScore);
            }

        }
但是,它不是
if(avgScoresMap.containsKey(wordKey))
,它应该是
if(!avgScoresMap.containsKey(wordKey))
,因为您正在检查映射是否包含密钥。如果不这样做,它就永远不会将数据放入
HashMap
。或者换句话说,这应该是您的方法:

public void computeWordValue(ArrayList<String> reviews) {

        // Initialize any needed HashMaps
        HashMap<String, Integer> totalScores = new HashMap<>();
        HashMap<String, Integer> wordCount = new HashMap<>();
        // Compute the word total scores and counts using the appropriate method.
        computeScoreAndCounts(reviews, totalScores, wordCount);
        // Build a HashMap of average scores
        HashMap<String, Double> avgScoresMap = new HashMap<>();//I'm assuming this is empty

        for (String wordKey : reviews) {
            
            if (!avgScoresMap.containsKey(wordKey)) {
                double avgScore = (double) (totalScores.get(wordKey) / wordCount.get(wordKey)); //I think this is why it doesn't work
                avgScoresMap.put(wordKey, avgScore);
            }

        }
        System.out.println(avgScoresMap);
    }
public void computeWordValue(ArrayList审查){
//初始化任何需要的哈希映射
HashMap totalScores=新HashMap();
HashMap wordCount=新HashMap();
//使用适当的方法计算单词总分和计数。
计算CoreandCounts(评论、总分、字数);
//构建平均分数的哈希图
HashMap avgScoresMap=newhashmap();//我假设它是空的
for(字符串wordKey:reviews){
如果(!avgScoresMap.containsKey(wordKey)){
double avgScore=(double)(totalScores.get(wordKey)/wordCount.get(wordKey));//我想这就是它不起作用的原因
avgScoresMap.put(wordKey,avgScore);
}
}
系统输出打印LN(avgScoresMap);
}