写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串。
package test; import java.util.Scanner; public class exam04 { public static void main(String[] arg) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String str = scanner.next(); System.out.println(reserveStr1(str)); reserveStr2(str); reserveStr3(str); } } public static StringBuilder reserveStr1(String str) { // 方法1 StringBuilder sb = new StringBuilder(str); sb.reverse(); return sb; } public static void reserveStr2(String str) { // 方法2:输入的数放入char数组中,按逆序放置 int len = str.length(); char[] ch = new char[len]; for (int i = 0; i < len; i++) { ch[len - i - 1] = str.charAt(i); } System.out.println(String.valueOf(ch)); } public static void reserveStr3(String str) { // 方法3 char[] chars = str.toCharArray(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < chars.length; i++) { sb.append(chars[chars.length - i - 1]); } System.out.println(sb.toString()); } }