java 数组 逆序排列

来源:百度知道 编辑:UC知道 时间:2024/09/21 15:42:43
用 Arrays
从键盘接受5个字符.
逆序排列
比如 :我爱你北京
结果为:京北你爱我
2

import java.util.*;
public class Ni
{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String str=sc.next();

for(int i=str.length()-1;i>=0;i--){
System.out.print(str.charAt(i));
}

}
}

//当直接回车时候程序结束
public class Test {

/**
* @param args
*/
public static void main(String[] args) {

BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str=null;
StringBuffer sb=null;
try{
str=bf.readLine();
sb=new StringBuffer(str);
}catch(Exception e){}
while(str.length()!=0)
{
System.out.println(sb.reverse());
try{
str=bf.readLine();
sb=new StringBuffer(str);
}catch(Exception e){}
}
}

}