406. 遞迴字串計算

  1. 請使用遞迴撰寫一個類別方法,可計算一個字串內有幾個 A,大小寫有所區別。
  2. 程式執行時,顯示【Input a string: 】要求輸入字串。
  3. 連續執行兩次,結果參考Screen Dump,顯示【輸入的字串 has X As】。將計算此字串內有幾個 A,代入 X 中。

執行結果

Input a string: COMPUTER SKILLS FOUNDATION
COMPUTER SKILLS FOUNDATION has 1 As
Input a string: java 6
java 6 has 0 As

程式碼

JPA04.java

import java.util.Scanner;

public class JPA04 {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String args[]) {
        prompt();
        prompt();
    }

    public static void prompt() {
        System.out.println("Input a string: ");
        String input = scanner.nextLine();

        System.out.printf("%s has %d As\n", input, countA(input));
    }

    public static int countA(String str) {
        if (str.isEmpty()) {
            return 0;
        }
        if (str.startsWith("A")) {
            return 1 + countA(str.substring(1));
        }
        return countA(str.substring(1));
    }
}

開始練習

import java.util.Scanner;

public class JPD04 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String args[]) {
      String s; 
      System.out.print("Input a string: ");
      s = keyboard.nextLine();
      System.out.printf("%s has %d As\n", s, countA(s));
      System.out.print("Input a string: ");
      s = keyboard.nextLine();
      System.out.printf("%s has %d As\n", s, countA(s));
    }

    public static int countA(String str) {
          if (str.equals(____)) {
              return 0;
          } else if (str.substring(__, __).equals("A")) {
              return ______________________;
          } else {
              return countA(str.substring(1));
          }
      }
}

results matching ""

    No results matching ""