Java 计算循环中每个字母的出现次数,并显示出现次数最多的字母

Java 计算循环中每个字母的出现次数,并显示出现次数最多的字母,java,loops,Java,Loops,我在使用网上找到的代码时遇到了问题。我的目标是计算一个字母显示的次数,并显示出现次数最多的字母,如果有两个或更多的字母出现在相同的次数,那么它们都会出现 这是我当前的输出: 以下是我在网上找到并使用的代码: public void fcount(String str) { int[] occurence = new int[255]; // Scanner scanner = new Scanner(System.in); // str = scanner.nextLine(); /

我在使用网上找到的代码时遇到了问题。我的目标是计算一个字母显示的次数,并显示出现次数最多的字母,如果有两个或更多的字母出现在相同的次数,那么它们都会出现

这是我当前的输出:

以下是我在网上找到并使用的代码:

public void fcount(String str)
{

int[] occurence = new int[255];
   // Scanner scanner = new Scanner(System.in);
// str = scanner.nextLine();

// optional to put eveyting in uppercase
str = str.toUpperCase();
// convert to char
char[] digit = str.toCharArray();
// count
for(int i = 0; i < digit.length; i++)

    occurence[digit[i]]++;
// find max
int max = 0;           // max value
char maxValue = 0;      // max index
for(int i = 0; i < occurence.length; i++) 

    {
   // new max ?
   if(occurence[i] > max) {
      max = occurence[i];
      maxValue = (char) i;
   }
}
// result
System.out.println("Character used " + max + " times is: " + (char) maxValue);
   // return "";
}
public void fcount(字符串str)
{
int[]发生率=新int[255];
//扫描仪=新的扫描仪(System.in);
//str=scanner.nextLine();
//可选择将eveyting大写
str=str.toUpperCase();
//转换成字符
字符[]位=str.toCharArray();
//计数
对于(int i=0;i最大值){
最大值=发生率[i];
maxValue=(char)i;
}
}
//结果
System.out.println(“使用字符“+max+”的次数为:”+(char)maxValue);
//返回“”;
}
这是我使用它的循环:

public void calpha()
{

char startUpper = 'A';
String cones = null;

for (int i = 0; i < 12; i++) {

    cones = Character.toString(startUpper);
    System.out.println(startUpper);

}
fcount(cones);

}
public void calpha()
{
char startUpper='A';
字符串=空;
对于(int i=0;i<12;i++){
锥体=字符。toString(startUpper);
系统输出打印项次(startUpper);
}
fcount(锥体);
}

您的
int[]occurrence=new int[255]中存在问题语句和用法:
出现[digit[i]+
可能导致
索引自动边界异常
,因为字符类型最多为2^16

循环中存在错误:

cones = Character.toString(startUpper);
您只是重新分配了
cones
的值,因此
fcount
接收一个只包含最后一个字符的字符串

解决办法是

cones += Character.toString(startUpper);

您的代码无法处理非ANSII字符。我的是

import java.util.*;

class Problem {
    public static void main(String args[]) {
        final String input = "I see trees outside of my window.".replace(" ", "");

        final List<Character> chars = new ArrayList<>(input.length());
        for (final char c : input.toCharArray()) {
            chars.add(c);
        }

        int maxFreq = 0;
        final Set<Character> mostFrequentChars = new HashSet<>();
        for(final char c : chars) {
            final int freq = Collections.frequency(chars, c);
            if (freq > maxFreq) {
                mostFrequentChars.clear();
                mostFrequentChars.add(c);
                maxFreq = freq;
            }
            else {
                if (freq == maxFreq) {
                    mostFrequentChars.add(c);
                }
            }
        }

        for (Character c : mostFrequentChars) {
            System.out.println(c);
        }
    }
}
import java.util.*;
阶级问题{
公共静态void main(字符串参数[]){
final String input=“我在窗口外看到树。”.replace(“,”);
最终列表字符=新的ArrayList(input.length());
for(最终字符c:input.toCharArray()){
添加(c)项;
}
int maxFreq=0;
最终集mostFrequentChars=新HashSet();
for(最终字符c:字符){
最终整数频率=集合频率(字符,c);
如果(频率>最大频率){
mostFrequentChars.clear();
最频繁的字符。添加(c);
maxFreq=频率;
}
否则{
if(freq==maxFreq){
最频繁的字符。添加(c);
}
}
}
for(字符c:mostFrequentChars){
系统输出打印ln(c);
}
}
}
尝试以下代码:

public static void main(String[] args) throws IOException {
    char startUpper = 'A';
    String cones = "";
    for (int i = 0; i < 12; i++) {
        cones += Character.toString(startUpper);
        System.out.println(startUpper);
    }
    fcount(cones);
}

public static void fcount(String str) {
    HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
    HashSet<Character> letters = new HashSet<Character>();
    str = str.toUpperCase();
    //Assume that string str minimium has 1 char
    int max = 1;
    for (int i = 0; i < str.length(); i++) {
        int newValue = 1;
        if (hashMap.containsKey(str.charAt(i))) {
            newValue = hashMap.get(str.charAt(i)) + 1;
            hashMap.put(str.charAt(i), newValue);

            if (newValue>=max) {
                max = newValue; 
                letters.add(str.charAt(i));
            }
        } else {
            hashMap.put(str.charAt(i), newValue);
        }           
    }

    System.out.println("Character used " + max + " times is: " + Arrays.toString(letters.toArray()));
    // return "";
}
publicstaticvoidmain(字符串[]args)引发IOException{
char startUpper='A';
字符串“”;
对于(int i=0;i<12;i++){
锥体+=字符toString(startUpper);
系统输出打印项次(startUpper);
}
fcount(锥体);
}
公共静态无效fcount(字符串str){
HashMap HashMap=新的HashMap();
HashSet字母=新的HashSet();
str=str.toUpperCase();
//假设字符串str minimium有1个字符
int max=1;
对于(int i=0;i=最大值){
最大值=新值;
添加(str.charAt(i));
}
}否则{
hashMap.put(str.charAt(i),newValue);
}           
}
System.out.println(“使用的字符”+max+”的次数是:“+Arrays.toString(letters.toArray())”;
//返回“”;
}

那么问题出在哪里,代码正常工作。你把“A”放在
fcount
中,它告诉你,“A”是1time@JohnnyAW循环迭代12次,因此输出为:A。。所以它应该告诉我,我用了12次,我想在循环中,你实际上想要做的是:cones+=startUpper。你可能想要做
cones+=Character.toString(startUpper)
如果你希望
cones
包含12次“A”。@Zurc在这种情况下,你应该压缩你的
字符串。顺便说一句,你为什么不干脆把“AAAAAAA”放进
fcount
?为什么要使用循环?我猜函数假定输入(
str
)只包含ASCII字符(即最多128个不同的值)。我向JohnnyAW和BOND道歉,他们在我发布答案之前发表了评论。我没有重新加载页面,所以我没有看到您的评论