【问题标题】:a method that replaces the greater occurrence with new char?一种用新字符替换更大事件的方法?
【发布时间】:2019-06-14 10:25:48
【问题描述】:

练习:创建一个计算出现次数的方法,并将字符作为输入。 出现次数最多的字符被替换为进入方法的字符(new char)

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;
}

【问题讨论】:

  • 这似乎是对您的练习,而不是对我们而言。真正的问题是什么?
  • 你有什么问题?
  • 您好,stackoverflow 不是提供练习的网站,而是提供帮助的,如果您在工作中遇到问题,您可以告诉您尝试了什么,不明白什么。

标签: java arrays string char int


【解决方案1】:

这里有一个解决办法,试着理解一下,改进一下,祝你好运

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');
    }
}

【讨论】:

    【解决方案2】:

    你可以这样做:

    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);
    }
    

    【讨论】:

      【解决方案3】:

      谢谢大家。我是这样解决的:

      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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-29
        • 2016-01-19
        • 1970-01-01
        • 1970-01-01
        • 2012-09-25
        相关资源
        最近更新 更多