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
Java Recursion - counting Characters in a string
提问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 else
case, number
is 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 length
of 0
returning 0
.
此外,您的基本情况应该是字符串是否为空,并且 a length
of0
返回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:
一些东西:
The base case should probably be
str.length() == 0
, notstr.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.Your first
if
looks good; if it doesn't match the first character, return the result ofcountChar
applied to the rest of the string.Your second
if
isn't quite right; you want to return 1 plus the result ofcountChar
applied to the rest of the string.
基本情况应该是
str.length() == 0
,不是str.length() == 1
。虽然没有正确或错误的基本情况,但在这里更容易为空字符串获得正确的行为。您在长度为 1 的字符串的基本情况下的行为实际上是错误的;如果长度为 1 的字符串确实包含字符怎么办?那你就错过了。你的第一个
if
看起来不错;如果它与第一个字符不匹配,则返回countChar
应用于字符串其余部分的结果。你的第二个
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 number
retains its value in recursive calls. This isn't the case; every time you go into a new recursive call, the value of number
is reset to 0. number
is 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。将其设为全局或在每次调用中将值作为参数传递。