简体   繁体   English

如何遍历哈希图并在数组列表中找到字符串的最大值?

[英]How I go through a hashmap and find highest value of a string in a array list?

This is the code I have so far for my homework and I'm at a dead end here. 这是我到目前为止用于家庭作业的代码,我在这里陷入了困境。 The idea is tis is a scrabble like deal where i get a string of words and then I have to go through each word to get its total value based on a dictionary in a hash map containing letters and integer values. 这个想法是tis就像是拼字游戏一样,我得到一串单词,然后我必须遍历每个单词以基于包含字母和整数值的哈希图中的字典来获得其总值。 I'm no sure how to implement this an would appreciate some direction. 我不确定如何实现这一点,将不胜感激。

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;
    }
}

Okay, this code will not work until you go through it, read all of the comments, and fill in the missing parts. 好的,直到您仔细阅读所有注释并填写缺少的部分,该代码才能工作。 I am pretty sure I found every issue, and I even fixed a few for you. 我很确定我找到了每个问题,甚至为您解决了一些问题。 Please do ask if anything is unclear, all changes are documented in the comments and I would be happy to clarify. 请问是否有不清楚的地方,所有更改都记录在评论中,我很乐意澄清。 If it still doesn't work after you make the changes, we can go from there as I may have missed something. 如果您进行更改后仍然无法使用,我们可以从那里继续,因为我可能错过了一些东西。 All of the comments can be removed as you understand the changes. 当您了解所做的更改时,可以删除所有注释。

Also, it seems strange that you are passing in the Map of letterScores as a parameter, is this a requirement of your homework? 另外,您将letterScores映射作为参数传递似乎很奇怪,这是您作业的要求吗?

Note the formatting of this code, it is very important to keep your code clean and easy to read. 请注意该代码的格式,这对保持代码的清洁和易于阅读非常重要。 This ensures that others can understand your code quickly, and that you may spot bugs more quickly. 这样可以确保其他人可以快速理解您的代码,并且可以更快地发现错误。 These are Java coding conventions , something you should definitely read and adhere to. 这些是Java编码约定 ,您一定要阅读并遵守这些约定 If you are using an IDE such as eclipse, you can typically select all of the code (CTRL+A) and auto indent it (CTRL+I). 如果使用的是诸如eclipse之类的IDE,通常可以选择所有代码(CTRL + A)并自动缩进(CTRL + I)。 If you are using something else, you can google for the commands to do this. 如果您使用的是其他工具,则可以通过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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM