我如何浏览散列图并在数组列表中找到字符串的最高值?

时间:2013-11-22 15:22:59

标签: java list dictionary hashmap

这是我迄今为止作业的代码,我在这里走到了尽头。这个想法是一个拼字游戏,我得到一串单词,然后我必须通过每个单词来获得基于包含字母和整数值的哈希映射中的字典的总值。我不确定如何实现这一点,我会欣赏一些方向。

public class ScrabbleAssistant 
    { 
    int maxScore = 0;
    String bestWord = "";
    int totalScore = 0;
    String getBestWord(List<String> options, Map<Character, Integer> letterscores)
    { 
        for (int i = 0; i < options.size(); i++) 
     {
        totalScore = 0; 
     }
        for (int j = 0; j < letterscores.size(); j++) 

        if (totalScore > maxScore)
    {
        maxScore = totalScore;

        return totalScore;   

    }
    Integer getScore(String word, Map<Character, Integer>letterscores, Integer total)
    {
       for (int i = 0; i < letterscores.size(); i++) 
    {
       totalScore = total + letterscores.remove(i);
    }
        if (totalScore > maxScore)
    {
        maxScore = totalScore;
        bestWord + word;
    }
        return totalScore;
    }
}

1 个答案:

答案 0 :(得分:0)

好的,这段代码在你完成它之后才会起作用,阅读所有的评论,并填写缺少的部分。我很确定我发现了每一个问题,我甚至为你解决了一些问题。请问是否有任何不清楚的地方,所有更改都记录在评论中,我很乐意澄清。如果在进行更改后仍然无效,我们可以从那里开始,因为我可能错过了一些东西。在您了解更改后,可以删除所有注释。

另外,你传递letterScores Map作为参数似乎很奇怪,这是你作业的要求吗?

请注意此代码的格式,保持代码清洁和易于阅读非常重要。这可以确保其他人可以快速理解您的代码,并且您可以更快地发现错误。 These are Java coding conventions,你应该阅读和坚持的东西。如果您使用的是诸如eclipse之类的IDE,通常可以选择所有代码(CTRL + A)并自动缩进(CTRL + I)。如果您正在使用其他内容,则可以使用google命令执行此操作。

public class ScrabbleAssistant { 
    //int maxScore = 0;  
    //String bestWord = "";  moved into getBestWord(), does not need class scope
    //int totalScore = 0; moved into methods, class scope could cause issues

    // added public, do not forget to give your methods a scope, changed letterScores to camelCase for readability
    public String getBestWord(List<String> options, Map<Character, Integer> letterScores) {
        String bestWord = "";
        int maxScore = 0;
        int totalScore;

        for (int i = 0; i < options.size(); i++) {

            totalScore = //what method should go here? hint: calculates the score of the given word

            if (totalScore > maxScore) {
                maxScore = totalScore;
                bestWord = // what is the best word? hint: its the current index of options 
               //return totalScore; //this is the wrong place to make a return, and you can't return an int in a class that returns a String  
             }
        }
        return // what should you return here? hint: must be a string
    }

// added public, do not forget to give your methods a scope, chnaged letterScores to camelCase for readability, removed Integer total parameter, was unnecessary
    public Integer getScore(String word, Map<Character, Integer>letterScores) {
        int totalScore = 0;

        for (int i = 0; i < word.length(); i++)  { // you were iterating through the map of letter scores, not the actual String passed in
            // you should not remove, you should use get the score of the letter from the current index of the string
            // this is fixed for you
            totalScore = totalScore + (letterScores.get(word.charAt(i).intValue();
        }

        //if (totalScore > maxScore){  This logic should not be here, this method should return the total score
        //maxScore = totalScore;
        //bestWord + word;
        //}
        return totalScore;
    }
}