返回列表 發帖

[12/24複習]

本帖最後由 李泳霖 於 2022-12-24 16:36 編輯

TQC+ 408
題目說明:
請將檔案另存成JPA04.java,並編譯為JPA04.class
設計說明:
1.請使用遞迴設計一個類別方法,此方法能夠將字串反向。
2.程式執行時,顯示[Input a string: ]要求輸入字串。
3.連續執行兩次,如執行結果參考畫面將字串反向印出。
  1. import java.util.Scanner;
  2. public class JPD04 {
  3.     static Scanner keyboard = new Scanner(System.in);
  4.     public static void main(String args[]) {
  5.         String s, c;
  6.         System.out.print("Input a string: ");
  7.         s = keyboard.nextLine();
  8.         System.out.printf("%s\n", reverse(s));
  9.         System.out.print("Input a string: ");
  10.         s = keyboard.nextLine();
  11.         System.out.printf("%s\n", reverse(s));
  12.     }
  13.    
  14.     ...
  15. }
  16. /*
  17.      reverse("hello")
  18.      =reverse("ello")+"h"
  19.      =reverse("llo")+"e"+"h"
  20.      =reverse("lo")+"l"+"e"+"h"
  21.      =reverse("o")+"l"+"l"+"e"+"h"
  22.      =reverse("")+"o"+"l"+"l"+"e"+"h"
  23.      =""+"o"+"l"+"l"+"e"+"h"
  24.      ="olleh"
  25. */
複製代碼
TQC+ 410
題目說明:
請將檔案另存成JPA04.java,並編譯為JPA04.class
設計說明:
1.請使用遞迴設計一個類別方法,此方法能夠將一個字串內的某個字元換成另一個字元。例如輸入字串[windows],將字串中的w值替換成g值,輸出字串為[gindogs]。
2.程式執行時,顯示[Input a string: ]要求輸入字串,接續顯示[Input a character: ] 要求輸入[被替換]的字元,最後顯示[Input another character: ]要求輸入替換字元。
3.請利用replace()函數進行字串替換,顯示如執行結果參考畫面。
  1. import java.util.Scanner;
  2. public class JPD04 {
  3.     static Scanner keyboard = new Scanner(System.in);
  4.     public static void main(String args[]) {
  5.         String s, c1, c2;
  6.         System.out.print("Input a string: ");
  7.         s = keyboard.nextLine();
  8.         System.out.print("Input a character: ");
  9.         c1 = keyboard.nextLine();
  10.         System.out.print("Input another character: ");
  11.         c2 = keyboard.nextLine();
  12.         System.out.printf("%s\n", replace(s, c1, c2));
  13.     }
  14.    
  15.     ...
  16. }
複製代碼
/*
    replace("windows","w","g")
    ="g"+replace("indows","w","g")
    ="g"+"i"+replace("ndows","w","g")
    ="g"+"i"+"n"+replace("dows","w","g")
    ="g"+"i"+"n"+"d"+replace("ows","w","g")
    ="g"+"i"+"n"+"d"+"o"+replace("ws","w","g")
    ="g"+"i"+"n"+"d"+"o"+"g"+replace("s","w","g")
    ="g"+"i"+"n"+"d"+"o"+"g"+"s"+replace("","w","g")
    ="g"+"i"+"n"+"d"+"o"+"g"+"s"+""
    =gindogs
*/
istak.teach2@gmail.com

返回列表