Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Java遞迴程式2.java
- author:郭翔宇
- 版本2─研究遞迴函式的運作原理
- */
- package com.sample;
- import java.util.Scanner;
- public class Project27 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scanner = new Scanner(System.in);
- String s1 = "";
- System.out.printf("input : ");
- s1 = scanner.next();
- // 方法1
- char[] c1 = new char[s1.length()];
- for (int i = 0; i < s1.length(); i++) {
- c1[i] = s1.charAt(i);
- }
- System.out.printf("output :\n");
- reverseSentence(c1, 0);
- System.out.printf("\n");
- // 方法2
- // System.out.printf("output : ");
- // reverseSentence(s1.toCharArray(), 0);
- // System.out.printf("\n");
- }
- private static void reverseSentence(char[] c1, int i) {
- // throw new UnsupportedOperationException("Not supported yet.");
- // To change body of generated methods, choose Tools | Templates.
- if (i < c1.length) {
- // System.out.printf("c1[i] = %c, i = %d\n", c1[i], i);//正向輸出
- reverseSentence(c1, i + 1);
- System.out.printf("c1[i] = %c, i = %d\n", c1[i], i);// 逆向輸出
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment