输入一个字符串,输出时数字倒序。例如:输入"hello2345wo7654",输出则为"hello5432wo4567"

时间:2022-06-18 19:04:40
 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 }