public static String backFormat( String input ) {
String result = null;
if( input != null ) {
int length = input.length();
StringBuilder str = new StringBuilder( input );
char temp;
for( int i=0; i<length; i++ ) {
temp = str.charAt( i );
str.setCharAt( i, str.charAt( length-1 - i ) );
str.setCharAt( length-1 - i, temp );
}
result = str.toString();
}
return result;
}
为啥返回的还是没有逆序的样子?

解决方案 »

  1.   

    循环出错了 
       for( int i=0; i<=length/2; i++ ) 
      

  2.   

    new StringBuilder(str).reverse().toString()就可以逆序
      

  3.   

    另一种实现,仅供参考
    public class TestStringReverse { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub String s="abcdefg";
    System.out.println("字符串原来的顺序:"+s);
    System.out.println("逆序后的顺序:"+Reverse(s));
    } private static String Reverse(String s) {
    // TODO Auto-generated method stub
    char[] c=s.toCharArray();
    int start=0,end=c.length-1;
    while(start<end){
    c[start]^=c[end];
    c[end]^=c[start];
    c[start]^=c[end];
    start++;
    end--;
    }
    s=new String(c);
    return s;

    }}
    输出字符串原来的顺序:abcdefg
    逆序后的顺序:gfedcba
      

  4.   

    我觉得自己编一个挺好,这样有利于学习,我写的你参考一下:
    import java.io.*;
    public class Nixu
    {
        public static void main(String[] args)
        {
            String str="",sub;
            try
            {
               BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
               System.out.print("请输入一个字符串:");
               str=input.readLine();            
            }
            catch(IOException e){  }
            System.out.print("逆序显示结果:");
            for(int n=str.length();n>=1;n--)
            {
                sub=str.substring(n-1,n);
                System.out.print(sub);
            }
        }
    }
      

  5.   

    真的实现起来也没多难其实 String stringSort(String str){
    StringBuffer buffer=new StringBuffer();
    for(int i=str.length()-1;i>-1;i--)
    buffer.append(str.charAt(i));
    return buffer.toString();
    }