【问题标题】:Counting letter occurences in a character array in Java [closed]在Java中计算字符数组中的字母出现次数[关闭]
【发布时间】:2014-07-25 09:06:14
【问题描述】:

我有一个项目要求我计算 Java 中字母的出现次数,由于某种原因,程序的输出只会给我字母“a”的出现次数。这是一个我知道如何解决的问题。

public String convert(String U) {

  int aCount = 0;
  int bCount = 0;
  int c = 0;
  int d = 0;
  int e = 0;
  int f = 0;
  int g = 0;
  // ...
  // removed for brevity

  char[] count = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

  char counting = U.charAt(i);

  for (i = 0; i > U.length(); ++i) {

    if (counting == count[0]) 
      aCount = aCount + 1;    

    if (counting == count[1]) 
       bCount = bCount + 1;

    if(counting == count[2]) 
      c++;

    if(counting == count[3]) 
      d++;

    if(counting == count[4]) 
      e++;

    if(counting == count[5]) 
      f++;

    if(counting == count[6]) 
      g++;

    // removed for brevity.
    // ...

    System.out.println(aCount + bCount + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r + s + t + u + v + w + x + y + z);
  }

  return"";
}

【问题讨论】:

  • for (i = 0; i >counting.length(); ++i) 是什么意思?计数是一个字符,它怎么会有长度。你甚至编译过这段代码吗?并且不应该 count=U.charAt(i) 在 for 循环内?
  • 你的 for 循环永远不会运行,除非 U.length() 神奇地小于 0....
  • 您将counting.length() 固定为U.length(),恭喜,您的for 循环运行,您计算的字符现在应该在for 循环内。
  • @Kartik_Koro for 循环运行? i > U.length() 是什么时候?当i 为 0 或 1 时肯定不会。
  • @MikeKoch 哦,我的错,我只是注意到他试图找到一个字符的长度(),我什至没有注意到错误的符号。是的,必须是 (i = 0; i

标签: java arrays sorting char


【解决方案1】:

每次循环迭代时您都不会得到一个新字符,并且每次循环迭代都会更新i 变量的计数器。应该是不同的迭代索引变量

// not here. INSIDE the loop
// char counting = U.charAt(i)
for( int index = 0; index < U.length(); ++index )
{
    char counting = U.charAt(index)
    if( counting == count[0] ) ++a;
    else if( counting == count[1] ) ++b;
    else if( counting == count[2] ) ++c;
    // ...
}

或者您可以使用 foreach 循环,它更简洁,意图更清晰。

for( char counting : U.toCharArray() )
{
    if( counting == count[0] ) ++a;
    else if( counting == count[1] ) ++b;
    else if( counting == count[2] ) ++c; 
    // ...
}

【讨论】:

    【解决方案2】:

    你总是得到counting 位置i 的字符。好吧,i 是一个 int(计算字符“i”的出现次数,我猜是在“为简洁起见”部分定义的),初始化为 0,所以你总是得到U 的第一个字符...我相信你的 IDE 已经警告过你了……

    counting = ... 放入循环并将for (int i = ...) 更改为for (int loopCounter = ...) 应该可以解决此问题:

    for (int loopCounter = 0; loopCounter < U.length; loopCounter++) {
        char counting = U.charAt(loopCounter);
        ...
    }
    

    【讨论】:

    • i 的好消息
    【解决方案3】:

    示例程序实际打印总和或所有小写字符出现。如果这是要求,那么它可以简单地完成(比如字符串由 str 表示)

    int charCountSum = str.replaceAll("[^a-z]", "").length();
    

    但是如果你想要小写a-z的单个字符出现那么

    Map<Character, Integer> occuranceCounter = new HashMap<Character, Integer>();
    int count;
    for(Character c : str.replaceAll("[^a-z]", "").toCharArray()){
        count = 0;
        if(occuranceCounter.containsKey(c)){
            count = occuranceCounter.get(c);
        }
        occuranceCounter.put(c, ++count);
    };
    
    for(char c : "abcdefghijklmnopqrstuvwxyz".toCharArray()){
        int times = occuranceCounter.containsKey(c) ? occuranceCounter.get(c) : 0;
        System.out.println(c + " occurs " + times + " times.");
    }
    

    【讨论】:

      【解决方案4】:

      这一行你已经放在了迭代 U 字符串的外部循环

      char counting = U.charAt(i);
      

      这就是为什么你只得到A's count

      保持在循环中

      并且还使用 if else 而不是 if always。

      所以应该像下面这样

      for (i = 0; i < U.length(); ++i) 
      {
      char counting = U.charAt(i);
      
        if (counting == count[0]) 
        {
          aCount = aCount + 1;    
        } 
        else if (counting == count[1]) 
          {
           bCount = bCount + 1;
          }
        else if(counting == count[2]) 
          {
              c++;
          }
      // and so on
      
      }
      

      【讨论】:

      • for 循环永远不会在这种情况下运行,因为i 永远不会大于U.length()
      • 并且i 也被用作'i's 数量的迭代变量和计数器
      猜你喜欢
      • 2017-04-18
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 2023-01-28
      相关资源
      最近更新 更多