- import java.util.Scanner;
- public class JPD04 {
- static Scanner keyboard = new Scanner(System.in);
- public static void main(String args[]) {
-
- String str;
- System.out.println("Input a string:");
- str = keyboard.nextLine();
- System.out.println(str+"has "+ countA(str) + "As");
- System.out.println("Input a string:");
- str = keyboard.nextLine();
- System.out.println(str+"has "+ countA(str) + "As");
- }
-
- public static int countA(String str) {
- // HAHA
-
- //1.input:HAHA, H => else
- //2.input:AHA, A => else if
- //3.input:HA, H => else
- //4.input:A, A => else if
- //2.input:, ""=> if
- if(str.equals("")) //切割完的時候
- {
- return 0;
- }
-
- else if(str.substring(0,1).equals("A")) //切割當有A的時候個數+1
- {
- return 1 + countA(str.substring(1));
- }
-
- else{ //切割 但是沒有A的內容 所以往下一個繼續做切割
- return countA(str.substring(1));
- }
-
- }
-
-
-
- }
複製代碼 |