看了两遍还是没看懂
public class StringBufferTest {
public static void main(String[] args) {
// 方式1
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String line = sc.nextLine();
char[] chs = line.toCharArray();
// for (int x = chs.length - 1; x >= 0; x--) {
// System.out.print(chs[x]);
// }
// 方式2
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String line = sc.nextLine();
StringBuffer sb = new StringBuffer(line);
// StringBuffer sb = new StringBuffer();
// sb.append(line);
sb.reverse();
String result = new String(sb);
String result = sb.toString();
System.out.println(result);
String result2 = new StringBuffer(line).reverse().toString();
System.out.println(result2);
|
|