递归函数递减计数4->0,然后递增计数0>4,如何停止递增计数
Recursive function counts down 4->0, but then counts up 0>4, how can I stop the count up
此代码的目的是使用递归函数确认字母 A 准确出现 4 次。我可以让它正确计数,但一旦它开始离开递归堆栈,它就会 +1 而不是 -1(我认为是因为它正在离开堆栈)。
有没有更好的方法来处理这个问题,我很困惑。
public class App {
public static boolean isPresentNTimes(String sequence, char marker, int count) {
System.out.println("This is the count: " + count);
if (sequence.isEmpty() != true){
if(sequence.charAt(0) == marker) {
isPresentNTimes(sequence.substring(1), marker, count-1);
System.out.println("The count is" + count);
}
else {
isPresentNTimes(sequence.substring(1), marker, count);
}
}
if (count == 4){
return true;
} else {
return false;
}
}
public static void main(String []args){
String seq1 = "ABBAACBA";
System.out.println(isPresentNTimes(seq1, 'A', 4));
}
}
这就是您实际想要实现的:
public static boolean isPresentNTimes(String sequence, char marker, int count) {
if(count < 0)
return false;
else if(count == 0 && sequence.isEmpty())
return true;
else if (!sequence.isEmpty()){
if(sequence.charAt(0) == marker) {
System.out.println("The count is " + count );
count--;
}
return isPresentNTimes(sequence.substring(1), marker, count);
}
else
return false;
}
从count=4
开始,每找到一个等于标记的元素就递减。确保在每次递归调用之前添加 return (i.e., return isPresentNTimes
(...)):
在您比较的代码中
if (count == 4){
return true;
} else {
return false;
}
if count == 4,这没有意义,因为您已经从 count = 4 开始了。
希望这会给你答案 ->
static boolean ispresnt(String word, char c, int count) {
if (word.length() == 0) {
if (count == 0) {
return true;
}
return false;
}
return word.charAt(0) == c ?
ispresnt(word.substring(1), c, count - 1) : ispresnt(word.substring(1), c, count);
}
此代码的目的是使用递归函数确认字母 A 准确出现 4 次。我可以让它正确计数,但一旦它开始离开递归堆栈,它就会 +1 而不是 -1(我认为是因为它正在离开堆栈)。
有没有更好的方法来处理这个问题,我很困惑。
public class App {
public static boolean isPresentNTimes(String sequence, char marker, int count) {
System.out.println("This is the count: " + count);
if (sequence.isEmpty() != true){
if(sequence.charAt(0) == marker) {
isPresentNTimes(sequence.substring(1), marker, count-1);
System.out.println("The count is" + count);
}
else {
isPresentNTimes(sequence.substring(1), marker, count);
}
}
if (count == 4){
return true;
} else {
return false;
}
}
public static void main(String []args){
String seq1 = "ABBAACBA";
System.out.println(isPresentNTimes(seq1, 'A', 4));
}
}
这就是您实际想要实现的:
public static boolean isPresentNTimes(String sequence, char marker, int count) {
if(count < 0)
return false;
else if(count == 0 && sequence.isEmpty())
return true;
else if (!sequence.isEmpty()){
if(sequence.charAt(0) == marker) {
System.out.println("The count is " + count );
count--;
}
return isPresentNTimes(sequence.substring(1), marker, count);
}
else
return false;
}
从count=4
开始,每找到一个等于标记的元素就递减。确保在每次递归调用之前添加 return (i.e., return isPresentNTimes
(...)):
在您比较的代码中
if (count == 4){
return true;
} else {
return false;
}
if count == 4,这没有意义,因为您已经从 count = 4 开始了。
希望这会给你答案 ->
static boolean ispresnt(String word, char c, int count) {
if (word.length() == 0) {
if (count == 0) {
return true;
}
return false;
}
return word.charAt(0) == c ?
ispresnt(word.substring(1), c, count - 1) : ispresnt(word.substring(1), c, count);
}