一种方法,用新的char代替更大的出现?

时间:2019-06-14 10:25:48

标签: java arrays string char int

锻炼:创建一种计算出现次数并且还以字符作为输入的方法。 出现次数最多的字符将替换为输入方法的字符(新字符)

public void myMethod() {

    String text = "ovolollo";

    int numberOfLetterA = ProvaEsercizio9.countCharOccurrences(text, 'a');
    ecc..

    System.out.println("Lettera a = " + numberOfLetterA);   
    ecc..   
}


public static int countCharOccurrences(String source, char target) {
    int counter = 0;

    for (int i = 0; i < source.length(); i++) {
        if (source.charAt(i) == target) {       
            counter++;
        }
    }
    return counter;
}

3 个答案:

答案 0 :(得分:0)

这是一个解决方案,请尝试理解它并加以改善,祝您好运

public class Main
{
    public static void myMethod(String text, char x) {
        int pos = 0;
        int max = 0;
        int tmp = 0;

        for (int i = 0; i < text.length(); i++) {
            tmp = countCharOccurrences(text, text.charAt(i));
            if (tmp > max) {  
                pos = i;
                max = tmp;
            }
        }
        System.out.println("The char at :{" + pos + "} with the value :{" + text.charAt(pos) + "} is the char with most occur");
    }


    public static int countCharOccurrences(String source, char target) {
        int counter = 0;

        for (int i = 0; i < source.length(); i++) {
            if (source.charAt(i) == target) {       
                counter++;
            }
        }
        return counter;
    }
    public static void main(String[] args) {
        myMethod("ovololloll", 'x');
    }
}

答案 1 :(得分:0)

您可以执行以下操作:

private static String replaceMostFrequentChar(String text, char withChar) {
    // count the number of occurrences of each character in `text`
    Map<Character,Integer> counts = new HashMap<>();
    for (char c: text.toCharArray()) {
        Integer count = counts.get(c);
        if (count == null) {
            count = 1;
        } else {
            count++;
        }
        counts.put(c, count);
    }
    // find one of the most frequent character
    char maxChar = 0;
    int maxCount = 0;
    for (Map.Entry<Character,Integer> e: counts.entrySet()) {
        char c = e.getKey();
        int count = e.getValue();
        if (count > maxCount) {
            maxChar = c;
            maxCount = count;
        }
    }
    // replace and return
    return text.replace(maxChar, withChar);
}

答案 2 :(得分:0)

谢谢大家。我这样解决了:

public class ProvaEsercizio9 {

    public void myMethod(String parola, char c) {
        char myChar = ' ';
        int max = 0;

        for (int i = 0; i < parola.length(); i++) {
            if ( ProvaEsercizio9.countCharOccurrences(parola, parola.charAt(i)) > max) {
                max=ProvaEsercizio9.countCharOccurrences(parola, parola.charAt(i));
                myChar=parola.charAt(i);
            }
        }

        for (int i = 0; i < parola.length(); i++) {
            if(parola.charAt(i)==myChar)
                parola=parola.replace(myChar, c);
        }
        System.out.println(parola);
    }


    public static int countCharOccurrences(String source, char target) {
        int counter = 0;

        for (int i = 0; i < source.length(); i++) {
            if (source.charAt(i) == target) {
                counter++;
            }
        }
        return counter;
    }
}