使用者自行輸入一個字串
將字串倒轉- [hide]import java.util.Scanner;
- public class JPD04 {
- static Scanner keyboard = new Scanner(System.in);
- public static void main(String args[]) {
- String s, c;
- System.out.print("Input a string: ");
- s = keyboard.nextLine();
- System.out.printf("%s\n", reverse(s));
- System.out.print("Input a string: ");
- s = keyboard.nextLine();
- System.out.printf("%s\n", reverse(s));
- }
-
- public static String reverse (String s)
- {
- // HAHA
- // AHA+H
- // HA +HA
- // A +HAH
- // "" + HAHA
-
-
- //H AHA
- //AHA H
- //HA HA
- //A HAH
- if(s.equals(""))
- {
- return "";
- }
- else
- {
- return reverse(s.substring(1))+s.substring(0,1);
- }
- }
- }[/hide]
複製代碼 |