Java 是否显示字符串中每个字母的出现情况?

Java 是否显示字符串中每个字母的出现情况?,java,Java,我知道我需要利用我的整数计数,但我不知道如何做到这一点。我们的教授让我们使用这个: Occurrence of h: 1 Occurrence of e: 2 Occurrence of l: 2 Occurrence of o: 3 etc.. 我不知道在这个小项目中应该在哪里使用它们 编辑:更新 c[26]; c[ch - 'a']++; publicstaticvoidmain(字符串[]args){ 扫描仪s=新的扫描仪(System.in); System.out.

我知道我需要利用我的整数计数,但我不知道如何做到这一点。我们的教授让我们使用这个:

Occurrence of h: 1
Occurrence of e: 2
Occurrence of l: 2
Occurrence of o: 3
etc..
我不知道在这个小项目中应该在哪里使用它们

编辑:更新

    c[26];
    c[ch - 'a']++;
publicstaticvoidmain(字符串[]args){
扫描仪s=新的扫描仪(System.in);
System.out.println(“键入要查找其字母的句子”);
字符串语句=s.nextLine();
字母(句子);
}
公共静态无效字母(字符串句子){
句子=句子。toLowerCase();
整数计数[];
char ch[]=句子.toCharArray();
for(int i=0;i<句子长度();i++){
系统输出打印(ch[i]);
}
字符字母表[]=“abcdefghijklmnopqrstuvxyz”.toCharArray();
System.out.println();
}
}

使用
哈希映射来跟踪。键是唯一的字符,整数计算您看到它的次数

    public static void main(String[] args) {
    Scanner s = new Scanner(System.in);

    System.out.println("type the sentence you want to find the letters of");
    String sentence = s.nextLine();

    getLetters(sentence);

}

public static void getLetters(String sentence) {
    sentence = sentence.toLowerCase();
    int count[];

    char ch[] = sentence.toCharArray();

    for (int i = 0; i < sentence.length(); i++) {
        System.out.print(ch[i]);
    }

    char alphabet[] = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    System.out.println();


}

我看不出有什么理由在这里使用HashMap。HashMaps使用HashFunction将一些值映射到内存中的位置,以便更快地访问。在这种情况下,他将有相同的,或非常类似的东西,数组和这个映射函数,是给他的(ch-'a')。另外,对于正在这样做的人来说,现在使用HashMap可能还为时过早

你们的问题是你们还不明白这个想法。 java中的字母有值(您可以检查ASCII表)。字母表中有26个字母,第一个是“a”,最后一个是“z”。所以你需要26个元素的数组。每次当字符串中有“a”时,您都希望在数组中增加0处的元素,当您进入“b”时,您希望增加1处的元素。。。。当你来到“z”元素25。所以,事实上,用(ch-'a')将字母映射到数组中的适当位置,其中是字母出现的次数


你拿字符串,对其进行.toLowerCase()大小写,传递一次以计数字母,然后打印你发现的内容。

为什么不试试哈希映射呢?如果教授告诉你创建一个容量为26的计数器数组
c
,并且因为他使用
ch-'a'
来确定字母索引,然后,您必须假设只应使用小写字母。在这种情况下,
h
的计数应该是两个:
Hello->Hello
@IanMc基于预期输出的语句,问题似乎是只计算小写基本拉丁字母。在任何情况下,问题标题、函数名和变量名中都会有更多或正确的词;我不是在找HashMap。我觉得你的答案很有用。我已经将字符串转换成小写,创建了一个字母表数组,但我一直在思考如何计算每个字母。这就是我到目前为止(主要)你们再次错过的要点。。。您的数组引用为26 int。初始所有元素都将为0。每次从字符串中提取字符时,都会出现[ch-'a']++。提示:
ch-'a'
是字符代码行上从“a”到ch的距离。(另一方面,虽然显然与指令冲突,但字母表数组是一种更通用、更可读的解决方案。)
    public static void main(String[] args) {
    Scanner s = new Scanner(System.in);

    System.out.println("type the sentence you want to find the letters of");
    String sentence = s.nextLine();

    getLetters(sentence);

}

public static void getLetters(String sentence) {
    sentence = sentence.toLowerCase();
    int count[];

    char ch[] = sentence.toCharArray();

    for (int i = 0; i < sentence.length(); i++) {
        System.out.print(ch[i]);
    }

    char alphabet[] = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    System.out.println();


}
import java.util.HashMap;

public class J {

  public static void main(String[] args) {

    String string = "aaaabbbccd";

    HashMap<Character, Integer> map = frequency(string);

    System.out.println(map);
  }

  public static HashMap<Character, Integer> frequency(String string) {
    int length = string.length();
    char c;

    HashMap<Character, Integer> map = new HashMap<Character, Integer>();

    // loop thru string
    for (int i = 0; i < length; i++) {
      c = string.charAt(i);

      // If u have already seen that character,
      // Increment its count
      if (map.containsKey(c)) {
        map.put(c, map.get(c) + 1);

      // otherwise this is the first time u
      // have seen it, so set count to 1
      } else {

        map.put(c, 1);
      }
    }

    return map;
  }
}
{a=4, b=3, c=2, d=1}