import java.util.Scanner;
public class JPA406 {
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.println(s+" has "+countA(s)+" As");
System.out.print("Input a string: ");
s=keyboard.nextLine();
System.out.println(s+" has "+countA(s)+" As");
}
public static int countA(String str) {
if(str.equals("")){
return 0;
}
else if(str.substring(0,1).equals("A")){
return 1+countA(str.substring(1));
}
else{
return countA(str.substring(1));
}
}
}
406 遞迴字串計算
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言