本人刚学习java,所以解决问题的思路比较笨,以下程序作用是从控制台输入一串字符,然后把这串字符由尾到头输出如:输入abc 则输出cba下以程序代码这样写如何?(是不是很笨呢)。还有其它更好的方法吗?请大家指教下。class printSring{
public void printing(){
String str="";
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
str = reader.readLine();
}
catch(Exception e){}
System.out.println("you input string is : "+str);


String str2 = str;
String newstr="";
String oldstr="";
int count = str2.length();
char [] myarry = new char [count];
for(int i=0;i<str2.length();i++)
{
myarry[i]=str2.charAt(i);
}

for(int j=0;j<myarry.length;j++)
{
System.out.println(myarry[j]);
newstr+=myarry[j];
}

System.out.println("-----------------------------------");

for(int x=myarry.length-1;x>=0;x--)
{
System.out.println(myarry[x]);
oldstr+=myarry[x];
}
System.out.println(newstr);
System.out.println(oldstr); } }

解决方案 »

  1.   


    import java.io.*;
    public class Test{
        public static void printing(){
            String str="";
            try
            {
                BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                str = reader.readLine();
            }
            catch(Exception e){}
            System.out.println("you input string is : "+str);
            for(int i=str.length()-1;i>=0;i--)
            System.out.print(str.charAt(i));
        }
        public static void main(String[] args){
         printing();
    }
    }
      

  2.   

          String str2 = "abc";
            
               String newstr="";           for(int i=str2.length()-1;i>=0;i--)
                {
                 newstr=newstr+str2.charAt(i);
                 }
                System.out.println(newstr);
    我写了一个比较简单的,不知道符合楼主的意思不?
      

  3.   

    StringBuffer   a=new   StringBuffer( "abc "); 
    a.reverse(); 
    System.out.println(a); 
      

  4.   

    StringBuffer   a=new   StringBuffer( "abc "); 
    a.reverse(); 
    System.out.println(a); 非常赞同你也可以用substring(int beginIndex, int endIndex) 以循环逐个得到
      

  5.   

    public static void main(String[] args)
    {
        String s="abc";
        char x[]=s.toCharArray();
        String sum="";
        for(int i=x.length-1;i>=0;i--)
        {
    sum=sum+x[i];
        }
        System.out.println(sum);
    }
    希望对你有帮助
      

  6.   

    import java.util.Scanner;
    public class StringReverse {
    public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    StringBuffer sb=new StringBuffer(sc.nextLine());
    sb.reverse();
    System.out.println(sb);
    }}