静态方法,使用映射统计小写单词中字母的出现次数。

huangapple go评论95阅读模式
英文:

Static method to count occurrences of a letter in a lower case word using maps

问题

我需要翻译的部分如下:

  1. 我必须创建一个名为MakeMap的类其中包含一个名为countLetters()的静态方法该方法应该以一个由小写字母组成的单词作为参数并返回该单词中字母的计数映射
  2. 这是Main类的测试代码我不能编辑):
  3. import java.util.Map;
  4. import java.util.List;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. public class Main
  8. {
  9. public static void main(String [] args)
  10. {
  11. Map<Character, Integer> count = MakeMap.countLetters("hello");
  12. List<Character> letters = new ArrayList<Character>(count.keySet());
  13. Collections.sort(letters);
  14. for(Character letter : letters)
  15. System.out.println("There are " + count.get(letter) + " of the letter '" + letter + "'");
  16. }
  17. }
  18. 输出格式也必须如下所示
  19. $ java Main
  20. There are 1 of the letter 'e'
  21. There are 1 of the letter 'h'
  22. There are 2 of the letter 'l'
  23. There are 1 of the letter 'o'
  24. 到目前为止这是我在MakeMap类中尝试的内容我觉得只需要进行一些小的调整就可以使其正常工作
  25. import java.util.Map;
  26. import java.util.TreeMap;
  27. import java.util.ArrayList;
  28. import java.util.Collections;
  29. import java.util.HashMap;
  30. public class MakeMap
  31. {
  32. public static Map countLetters(String word)
  33. {
  34. HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>();
  35. String w = word.toLowerCase();
  36. // 检查strArray的每个字符
  37. for(int i = 0; i < word.length(); i++)
  38. {
  39. if(charCountMap.containsKey(word.charAt(i)))
  40. {
  41. // 如果字符在charCountMap中已存在,
  42. // 将其计数增加1
  43. charCountMap.put(word.charAt(i), charCountMap.get(word.charAt(i)) + 1);
  44. }
  45. else
  46. {
  47. // 如果字符在charCountMap中不存在,
  48. // 将该字符放入charCountMap中,其值为1
  49. charCountMap.put(word.charAt(i), 1);
  50. }
  51. }
  52. return charCountMap;
  53. }
  54. }
  55. 这是我收到的错误消息
  56. 注意Main.java使用了未经检查或不安全的操作
  57. 注意重新编译时请使用-Xlint:unchecked以获取详细信息
英文:

I have to create a class called MakeMap with a single static method called countLetters(). The method should take as a parameter a word formed of lowercase letters and return a map of the counts of the letters in the word.
This is the test code for the Main class (which I cannot edit):

  1. import java.util.Map;
  2. import java.util.List;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. public class Main
  6. {
  7. public static void main(String [] args)
  8. {
  9. Map&lt;Character, Integer&gt; count = MakeMap.countLetters(&quot;hello&quot;);
  10. List&lt;Character&gt; letters = new ArrayList&lt;Character&gt;(count.keySet());
  11. Collections.sort(letters);
  12. for(Character letter : letters)
  13. System.out.println(&quot;There are &quot; + count.get(letter) + &quot; of the letter &#39;&quot; + letter + &quot;&#39;&quot;);
  14. }
  15. }

The output must be formatted like this also:
$ java Main
There are 1 of the letter 'e'
There are 1 of the letter 'h'
There are 2 of the letter 'l'
There are 1 of the letter 'o'

This is what I have attempted so far in my MakeMap class. I feel like I only need a couple of minor adjustments to get this working.

  1. import java.util.Map;
  2. import java.util.TreeMap;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. public class MakeMap
  7. {
  8. public static Map countLetters(String word)
  9. {
  10. HashMap&lt;Character, Integer&gt; charCountMap = new HashMap&lt;Character, Integer&gt;();
  11. String w = word.toLowerCase();
  12. // checking each char of strArray
  13. for(int i = 0; i &lt; word.length(); i++)
  14. {
  15. if(charCountMap.containsKey(word.charAt(i)))
  16. {
  17. // If char is present in charCountMap,
  18. // incrementing it&#39;s count by 1
  19. charCountMap.put(word.charAt(i), charCountMap.get((word.charAt(i)) + 1 ));
  20. }
  21. else
  22. {
  23. // If char is not present in charCountMap,
  24. // putting this char to charCountMap with 1 as it&#39;s value
  25. charCountMap.put(word.charAt(i), 1);
  26. }
  27. }
  28. return charCountMap;
  29. }
  30. }

This is the error message I am getting:
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

答案1

得分: 0

不正确的括号,应为:

charCountMap.put(word.charAt(i), (charCountMap.get(word.charAt(i)) + 1));

此外,您必须从该方法中返回charCountMap,而不是在其中打印它,因此该方法必须定义为:

public static Map<Character, Integer> countLetters(String word)

英文:

Incorrect brackets, should be:

charCountMap.put(word.charAt(i), (charCountMap.get(word.charAt(i)) + 1));

additionaly you must return charCountMap from this method isntead of print it out there, so the method must be defined as public static Map&lt;Character, Integer&gt; countLetters(String word)

答案2

得分: 0

你有问题在这里:

  1. charCountMap.put(word.charAt(i), charCountMap.get((word.charAt(i)) + 1 ));

应该是:

  1. charCountMap.put(word.charAt(i), charCountMap.get((word.charAt(i))) + 1 );

还应该添加return charCountMap;

英文:

Hello You have problem here :

  1. charCountMap.put(word.charAt(i), charCountMap.get((word.charAt(i)) + 1 ));

Must be

  1. charCountMap.put(word.charAt(i), charCountMap.get((word.charAt(i))) + 1 );

And you should return charCountMap;

答案3

得分: 0

如已提到,countLetters() 在你的实现中返回 void;给定的代码期望它返回一个 HashMap<Character, Integer>。你可以在方法末尾返回你的 charCountMap,它会起作用。

一些额外的注意事项:

你有一行代码
String w = word.toLowerCase();
它没有任何作用,因为你之后从未使用过 w;你可能想要将它改为 word

最后,如果在循环中像这样做
char curr = word.charAt(i);
然后在使用 curr 而不是 word.charAt(i),可以轻松避免括号错误。这样更容易阅读,看起来更干净。

英文:

As already said, countLetters() returns void in your implementation; the given code expects it to return a HashMap&lt;Character, Integer&gt;. You can return your charCountMap at the end of the method and it'll work.

Some additional notes:

You have a line
String w = word.toLowerCase();.
that doesn't do anything, since you never use w again; you'll want to change it to word.

Lastly, the bracketing error could have easily been avoided if you did something like
char curr = word.charAt(i);
in the loop and then used curr instead of word.charAt(i). Makes it easier to read and looks cleaner.

huangapple
  • 本文由 发表于 2020年7月22日 19:38:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63033319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定