利用正则表达式求解
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String[] s = sc.nextLine().split(" |\\$|\\!|\\#|\\*|\\."); int i = 0, j = s.length - 1; while(i < j){ String temp = s[i]; s[i] = s[j]; s[j] = temp; i++; j--; } StringBuilder result = new StringBuilder(); for(String item : s){ result.append(item + " "); } System.out.println(result.toString().trim()); } }