用StringBuilder来实现经典的反转问题
import java.util.Scanner; public class Practise03 { public static void main(String[] args) { //键盘录入一个字符串,用方法进行反转 System.out.println("请输入一个字符串:"); Scanner sc = new Scanner(System.in); String str = sc.next(); System.out.println("反转后的字符是:"+reverse(str)); } public static String reverse(String str){ //经典反转四步骤 StringBuilder sb = new StringBuilder(); sb.append(str); sb.reverse(); return sb.toString(); } }