KuoHsiangYu

Java遞迴程式2.java

Sep 4th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. /*
  2. Java遞迴程式2.java
  3. author:郭翔宇
  4. 版本2─研究遞迴函式的運作原理
  5. */
  6. package com.sample;
  7.  
  8. import java.util.Scanner;
  9.  
  10. public class Project27 {
  11.  
  12.     public static void main(String[] args) {
  13.         // TODO Auto-generated method stub
  14.         Scanner scanner = new Scanner(System.in);
  15.         String s1 = "";
  16.         System.out.printf("input : ");
  17.         s1 = scanner.next();
  18.  
  19.         // 方法1
  20.         char[] c1 = new char[s1.length()];
  21.         for (int i = 0; i < s1.length(); i++) {
  22.             c1[i] = s1.charAt(i);
  23.         }
  24.         System.out.printf("output :\n");
  25.         reverseSentence(c1, 0);
  26.         System.out.printf("\n");
  27.  
  28.         // 方法2
  29.         // System.out.printf("output : ");
  30.         // reverseSentence(s1.toCharArray(), 0);
  31.         // System.out.printf("\n");
  32.     }
  33.  
  34.     private static void reverseSentence(char[] c1, int i) {
  35.         // throw new UnsupportedOperationException("Not supported yet.");
  36.         // To change body of generated methods, choose Tools | Templates.
  37.         if (i < c1.length) {
  38.             // System.out.printf("c1[i] = %c, i = %d\n", c1[i], i);//正向輸出
  39.             reverseSentence(c1, i + 1);
  40.             System.out.printf("c1[i] = %c, i = %d\n", c1[i], i);// 逆向輸出
  41.         }
  42.     }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment