package Recursive;
import java.util.Arrays;
import java.util.Scanner;
public class Exercise18_17 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a list of chars: ");
char[] chars = input.nextLine().toCharArray();
System.out.print("Enter a char: ");
char ch = input.next().charAt(0);
System.out.println("The number of char " + ch + " in char list " + Arrays.toString(chars) + " is " + count(chars, ch));
}
/** 返回數组中某个指定字符出现的次数 */
public static int count(char[] chars, char ch) {
return count(chars, ch, chars.length-1);
}
/** 返回數组中某个指定字符出现的次数辅助方法 */
public static int count(char[] chars, char ch, int high) {
int count = chars[high] == ch ? 1 : 0;
return high == 0 ? count : count + count(chars, ch, high-1);
}
}
08-14
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-21
1947
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-10
2341
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)