Java 递归 - 计算字符串中的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22615851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:53:26  来源:igfitidea点击:

Java Recursion - counting Characters in a string

javarecursion

提问by Freedom

I'm trying to find the number of occurrences "character" is found in "str" using recursion. I think I have the concept of what to do but for some reason the code does not work when I test it out...do you know why its wrong?

我正在尝试使用递归查找在“str”中找到“character”的出现次数。我想我有该做什么的概念,但由于某种原因,当我测试它时代码不起作用......你知道为什么它是错误的吗?

   public static int countChar(String str, String character) {
    int number = 0;
    if(str.length()==1) {
        return number;
    }
    if (!(str.substring(0,1).equals(character))) {
        return countChar(str.substring(1), character);
    } else {
        number = number + 1;
        return countChar(str.substring(1), character);
    }
}

采纳答案by Mzf

number is a local variable ....

number 是一个局部变量....

 public static int countChar(String str, String character) {
    if(str.length()==0) {
        return 0;
    }

    if ((str.substring(0,1).equals(character))) {
        return 1 + countChar(str.substring(1), character);
    }

    return countChar(str.substring(1), character);
}

The terminating case is when the String length is zero.

终止情况是字符串长度为零。

For each step , check the current char , if match - add 1 to the result for the rest of the string, if not return the match result for the rest of the string

对于每一步,检查当前字符,如果匹配 - 将字符串其余部分的结果加 1,如果不匹配,则返回字符串其余部分的匹配结果

回答by rgettman

In the elsecase, numberis ignored after it's incremented. But it's not needed anyway. Just add one to whatever the recursive call returns.

在这种else情况下,number增加后会被忽略。但无论如何都不需要。只需向递归调用返回的任何内容添加一个。

} else {
    return 1 + countChar(str.substring(1), character);
}

Also, your base case should be if the string is empty, with a lengthof 0returning 0.

此外,您的基本情况应该是字符串是否为空,并且 a lengthof0返回0

回答by G. Bach

Code for what rgettman specified:

rgettman 指定的代码:

public static int countChar(String str, String character) {

    if(str.length() == 0) {
        return 0;
    }

    if (!(str.substring(0,1).equals(character))) {
        return countChar(str.substring(1), character);
    } else {
        return 1 + countChar(str.substring(1), character);
    }
}

回答by Patrick87

A few things:

一些东西:

  1. The base case should probably be str.length() == 0, not str.length() == 1. While there's no right or wrong base case, it's easier here to get the right behavior for an empty string. Your behavior in the base case of length 1 strings is actually wrong; what if the length 1 string does contain character? Then you're missing it.

  2. Your first iflooks good; if it doesn't match the first character, return the result of countCharapplied to the rest of the string.

  3. Your second ifisn't quite right; you want to return 1 plus the result of countCharapplied to the rest of the string.

  1. 基本情况应该是str.length() == 0,不是str.length() == 1。虽然没有正确或错误的基本情况,但在这里更容易为空字符串获得正确的行为。您在长度为 1 的字符串的基本情况下的行为实际上是错误的;如果长度为 1 的字符串确实包含字符怎么办?那你就错过了。

  2. 你的第一个if看起来不错;如果它与第一个字符不匹配,则返回countChar应用于字符串其余部分的结果。

  3. 你的第二个if不太对;您想返回 1 加上countChar应用于字符串其余部分的结果。

It looks like you've got one misconception that's making this harder than it needs to be: the way the code is written, you think that numberretains its value in recursive calls. This isn't the case; every time you go into a new recursive call, the value of numberis reset to 0. numberis local to the function, which means that each recursive call gets its own copy to play with. If you want the recursive calls to get a value like that, you need to pass it as an argument, like you're doing with substrings.

看起来您有一个误解,使这变得比它需要的更难:编写代码的方式,您认为number它在递归调用中保留了它的价值。事实并非如此。每次进入新的递归调用时, 的值number都会重置为 0。number对于函数来说是本地的,这意味着每个递归调用都有自己的副本可供使用。如果您希望递归调用获得这样的值,则需要将其作为参数传递,就像处理子字符串一样。

回答by Sameer

First and foremost

首先也是最重要的

int number = 0;

整数编号 = 0;

Number is getting initialized to 0 on each call. Make it global or pass the value as a parameter in each call.

每次调用时,数字都会被初始化为 0。将其设为全局或在每次调用中将值作为参数传递。