【发布时间】:2017-10-18 21:19:55
【问题描述】:
我的作业是创建一个递归方法来计算给定字符串中给定字母的出现次数。到目前为止,这是我的代码:
import java.util.Scanner;
public class Exercise18_10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.next();
System.out.print("Enter a character: ");
String letter = sc.next();
char a = letter.charAt(0);
System.out.println("The count of " + a + " is: " + count(str, a));
}
public static int count(String str, char a) {
int count = str.indexOf(a);
return count;
}
}
在count 中,我使用indexOf 查找所需字母的第一个匹配项,但我不知道在那之后该怎么做。
【问题讨论】:
-
你知道
indexOf(...)是做什么的吗? -
是的,它找到指定字符的第一次出现
-
另外,你知道什么是递归吗?因为我什么都没看到。
-
这就是我寻求帮助的原因,我不知道如何从中制作递归方法
-
在 indexOf(a, 100);?
标签: java string recursion methods character