This is what you actually want to implement:
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;
}
Start with count=4
and decrement every time you found one element equals to mark. Make sure to add return before each recursive call (i.e., return isPresentNTimes
(...)):
In your code you were comparing
if (count == 4){
return true;
} else {
return false;
}
if count == 4, which does not make sense since you start already with count = 4.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…