你可以参照一下代码写:import java.util.Scanner; /* 需求:把字符串反转 * 举例:键盘录入"abc" * 输出结果:"cba"*/ public class StringBuffer4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入字符串:"); String str = sc.nextLine(); char[] c = str.toCharArray(); String s = ""; for (int i = c.length - 1; i >= 0; i--) { s = s + c[i]; } System.out.println(s); } } |