Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Java遞迴程式1.java
- author:郭翔宇
- 參考上述程式碼改寫完成使用Java語言利用遞迴函數進行正向輸入逆向輸出。
- 輸入(input):12345
- 輸出(output):54321
- */
- package com.sample;
- import java.util.Scanner;
- public class Project26 {
- public static void main(String[] args) {
- // TODO code application logic here
- 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 : ");
- 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.print(c1[i]);//正向輸出
- reverseSentence(c1, i + 1);
- System.out.print(c1[i]);// 逆向輸出
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement