輸入一個字符串,輸出時數字倒序。例如:輸入"hello2345wo7654",輸出則為"hello5432wo4567"


 1 public class ReserveString {
 2     public static void main(String[] args) {
 3         System.out.println("Please Input String:");
 4         Scanner sc = new Scanner(System.in);
 5         String a = sc.next();
 6         boolean state = false;
 7         StringBuffer sb = new StringBuffer();
 8         for (int i = 0; i < a.length(); i++) {
 9             String str = a.substring(i, i + 1);
10             if (str.matches("^[0-9]*$")) {
11                 state = true;
12                 sb.append(str);
13             } else {
14                 if (state) {
15                     System.out.print(sb.reverse().toString());
16                     state = false;
17                     sb.setLength(0);
18                 }
19                 System.out.print(str);
20             }
21         }
22         if (state) {
23             System.out.print(sb.reverse().toString());
24         }
25 
26     }
27 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM