用java语言编写程序,任意输入一个字符串,从最后一个字符开始倒序输出他。

2024-12-22 16:21:49
推荐回答(1个)
回答1:

自己参考下吧

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

public static void main(String[] args){

BufferedReader br = null;
String s = "";
String s1 = "";

try{

br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("请输入字符并以回车结束:");
//读入键盘输入
s = br.readLine();
StringBuffer ss = new StringBuffer(s);
ss.reverse();//StringBuffer类的逆序函数
s1 = ss.toString();
System.out.println("你输入的是:" + s);
System.out.println("逆序以后是:" + s1);
} catch (IOException e){
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
} finally{
try{
br.close();
}catch(Exception e){
e.printStackTrace();
}
}
}

}