英文:
Static method to count occurrences of a letter in a lower case word using maps
问题
我需要翻译的部分如下:
我必须创建一个名为MakeMap的类,其中包含一个名为countLetters()的静态方法。该方法应该以一个由小写字母组成的单词作为参数,并返回该单词中字母的计数映射。
这是Main类的测试代码(我不能编辑):
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class Main
{
public static void main(String [] args)
{
Map<Character, Integer> count = MakeMap.countLetters("hello");
List<Character> letters = new ArrayList<Character>(count.keySet());
Collections.sort(letters);
for(Character letter : letters)
System.out.println("There are " + count.get(letter) + " of the letter '" + letter + "'");
}
}
输出格式也必须如下所示:
$ 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'
到目前为止,这是我在MakeMap类中尝试的内容。我觉得只需要进行一些小的调整就可以使其正常工作。
import java.util.Map;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class MakeMap
{
public static Map countLetters(String word)
{
HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>();
String w = word.toLowerCase();
// 检查strArray的每个字符
for(int i = 0; i < word.length(); i++)
{
if(charCountMap.containsKey(word.charAt(i)))
{
// 如果字符在charCountMap中已存在,
// 将其计数增加1
charCountMap.put(word.charAt(i), charCountMap.get(word.charAt(i)) + 1);
}
else
{
// 如果字符在charCountMap中不存在,
// 将该字符放入charCountMap中,其值为1
charCountMap.put(word.charAt(i), 1);
}
}
return charCountMap;
}
}
这是我收到的错误消息:
注意:Main.java使用了未经检查或不安全的操作。
注意:重新编译时请使用-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):
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class Main
{
public static void main(String [] args)
{
Map<Character, Integer> count = MakeMap.countLetters("hello");
List<Character> letters = new ArrayList<Character>(count.keySet());
Collections.sort(letters);
for(Character letter : letters)
System.out.println("There are " + count.get(letter) + " of the letter '" + letter + "'");
}
}
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.
import java.util.Map;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class MakeMap
{
public static Map countLetters(String word)
{
HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>();
String w = word.toLowerCase();
// checking each char of strArray
for(int i = 0; i < word.length(); i++)
{
if(charCountMap.containsKey(word.charAt(i)))
{
// If char is present in charCountMap,
// incrementing it's count by 1
charCountMap.put(word.charAt(i), charCountMap.get((word.charAt(i)) + 1 ));
}
else
{
// If char is not present in charCountMap,
// putting this char to charCountMap with 1 as it's value
charCountMap.put(word.charAt(i), 1);
}
}
return charCountMap;
}
}
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<Character, Integer> countLetters(String word)
答案2
得分: 0
你有问题在这里:
charCountMap.put(word.charAt(i), charCountMap.get((word.charAt(i)) + 1 ));
应该是:
charCountMap.put(word.charAt(i), charCountMap.get((word.charAt(i))) + 1 );
还应该添加return charCountMap;
。
英文:
Hello You have problem here :
charCountMap.put(word.charAt(i), charCountMap.get((word.charAt(i)) + 1 ));
Must be
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<Character, Integer>
. 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论