package cn.itcast.all_test1;
import java.util.Scanner;
public class RevString {
/*
* 键盘录入字符串:如"abc" 将字符串进行反转reverse
* 循环输入字符串,当输入"end"时循环结束
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) { //循环输入字符串
System.out.println("请输入一个字符串");
String line = sc.nextLine();
if (line.equals("end")) { //判断条件:当输入"end"时结束循环语句
break;
} else {
System.out.println(revString(line));
}
}
}
private static String revString(String line) {
StringBuffer sb = new StringBuffer(line);
sb.reverse();
return sb.toString();
}
}
|
|