【问题标题】:Trouble with Loop to Count Number of Given Characters in a String循环计算字符串中给定字符数的问题
【发布时间】:2018-05-07 17:10:14
【问题描述】:

我必须使用提供的循环来计算字符“b”出现在 String FourthString 中的次数,并且由于某种原因它返回了不正确的数字。我相信这与 if 条件有关,但我可能会弄错。

任何帮助将不胜感激。

字符串:

String FourthString = "棒球棒";

它应该返回 3。

代码格式:

char target        ='b';                               
int  length        = fourthString.length( );               
int  count         = 0;                                
int  startNxtSrch  = 0;                                
int  foundAt       = 0;                               

while( ....... ) {
    foundAt = .......;
    if ( ....... ) {
        startNxtSrch = foundAt +1;
        count += 1;
    } else {
        startNxtSrch = length;
    }
}  
System.out.printf( "%nThere are %d occurrences of '%c' in fourthStr.%n", 
                   count,
                   target );

我尝试返回的数字不正确:

int i = 0;

while( i < length ) {
    foundAt = startNxtSrch;
    if ( fourthString.indexOf('b', startNxtSrch) != -1 ) {
        startNxtSrch = foundAt + 1;
        count += 1;
        i++;
    } else {
        startNxtSrch = length;
        i++;
    }
}
System.out.printf( "%nThere are %d occurrences of '%c' in fourthStr.%n", 
                   count,
                   target );

【问题讨论】:

  • 谢谢。我实际上已经查看了这篇文章,但它没有帮助,因为我的问题必须适合特定的格式,我相信这个问题与循环有关,而不是字符的计数,因此是标题。
  • 在我看来条件应该是startNxtSrch &lt; length。但我同意其他一些 cmets:这是一种非常复杂的方法。而且您可能不应该添加i 变量。我相信模板中已经存在所有必要的变量。

标签: java loops if-statement while-loop conditional


【解决方案1】:

这将为您提供正确的字符数:

char target        = 'b';
int  length        = fourthString.length( );
int  count         = 0;
int  startNxtSrch  = 0;
int  foundAt       = 0;

while(startNxtSrch < length) {
    foundAt = fourthString.indexOf(target,startNxtSrch);
    if (foundAt>=0) {
        startNxtSrch = foundAt + 1;
        count += 1;
    } else {
        startNxtSrch = length;
    }
}
System.out.printf( "%nThere are %d occurrences of '%c' in fourthStr.%n",
            count,
            target );

【讨论】:

    【解决方案2】:

    一个更好的方法是遍历整个数组并计算你的字符出现的次数。

    String fourthString = "a bat for baseball";
    char target = 'b';
    count = 0;
    for(int i = 0; i < fourthString.length; i++){
        if(fourthString.charAt(i) == traget){
            count++;
        }
    }
    System.out.println(count);
    

    【讨论】:

    • 完全同意。但是,我不是在寻找“更好的方法”,因为我需要遵循提供的格式。它过于复杂,但我只能更改所提供格式的......部分。
    【解决方案3】:

    你可以使用下面这行代码:

    int count = StringUtils.countMatches("the string you want to search in", "desired string");
    

    你的例子:

    int count = StringUtils.countMatches("a bat for baseball", "b");
    

    你可以在这里找到一个类似的问题和这个答案:https://stackoverflow.com/a/1816989/8434076

    编辑

    while(startNxtSrch != lenth)
    {
       foundAt = indexOf('b', startNxtSrch);
       if (foundAt != -1)
       {
          startNxtSrch = foundAt +1;
          count += 1;
       }  
       else
          startNxtSrch = length;
    } 
    

    【讨论】:

    • 感谢您的反馈,但格式必须遵循所提供的内容,我只能更改代码的.....部分。对我来说似乎也过于复杂。
    • 检查我编辑的答案。我想这就是你要找的:)
    【解决方案4】:

    这在我看来很复杂。

    如果我要编写这段代码,这里有一个更简单的解决方案:

    while( i < length ) {
        if (fourthString.contains("b")) {
            fourthString = fourthString.replaceFirst("b", "");
            count++;
        }
        i++;
    }
    
    System.out.printf( "%nThere are %d occurrences of '%c' in fourthStr.%n",
                    count,
                    target );
    

    【讨论】:

    • 我同意,它过于复杂,但我必须遵循提供的格式并且只更改代码的.....部分,这就是为什么我很难弄清楚它的原因。
    猜你喜欢
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 2013-09-11
    相关资源
    最近更新 更多