要对String的内置char[]数组有深刻理解。反过来排序,for循环效率最高
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNextLine()){ String li = in.nextLine(); char[] liArr = li.toCharArray(); int len = liArr.length; StringBuilder builder = new StringBuilder(); for(int i=0;i<len;i++){ builder.append(liArr[len-i-1]); } System.out.println(builder); } } }