import java.util.*; public class Solution { public String trans(String s, int n) { // write code here StringBuilder ansBuilder = new StringBuilder(); // ************************************************************* // 问题在这:如果分割符后面没有值,split("")不会继续切分,split("", -1)会继续切分。 String[] arr = s.split(" ", -1); int length = arr.length; // System.out.println(length); for(int i = length - 1; i >= 0; i--){ String str = arr[i]; // upper or lower for(int j = 0; j < str.length(); j++){ char tmp = str.charAt(j); // System.out.println(tmp); if(tmp >= 'a' && tmp <= 'z'){ tmp = (char)(tmp - ('a' - 'A')); }else{ tmp = (char)(tmp + ('a' - 'A')); } ansBuilder.append(tmp); // System.out.println(ansBuilder); } if(i > 0) ansBuilder.append(" "); } String ans = ansBuilder.toString(); return ans; } }