- package com.heima.stringbuffer;
- import java.util.Scanner;
- public class Test1 {
- /**
- 需求:把字符串反转
- 举例:键盘录入"abc"
- 输出结果:"cba"
-
- 用StringBuffer的功能实现
- */
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入一个字符串: ");
- String str = sc.nextLine();
- StringBuffer sb = new StringBuffer(str);
- sb.reverse();
- System.out.println(sb);
- }
- }
复制代码 |
|