将一字符串反转输出,帮我看看这个那出问题了,~~import java.lang.*;public class AA
{
public static void main(String args[])
{
String str = "Hello World!";
StringBuffer a = new StringBuffer(str);
String A= a.reverse();
System.out.println(A);
}
}
{
public static void main(String args[])
{
String str = "Hello World!";
StringBuffer a = new StringBuffer(str);
String A= a.reverse();
System.out.println(A);
}
}
所以:import java.lang.*; public class AA
{
public static void main(String args[]) {
String str = "Hello World!";
StringBuffer a = new StringBuffer(str);
StringBuffer A= a.reverse();
System.out.println(A);
}
}
返回值是一个StringBuffer类型的,
不是String类型,可以这样改:import java.lang.*; public class AA
{
public static void main(String args[])
{
String str = "Hello World!";
StringBuffer a = new StringBuffer(str);
String A= a.reverse().toString();
System.out.println(A);
}
} 在reverse()后面加一个toString()即可,可以运行了,
试一下。
更简单的String A = a.reverse().toString()
直接把翻转后的字符缓冲区转换为String类型
{
public static void main(String args[])
{
String str = "Hello World!";
StringBuffer a = new StringBuffer(str);
StringBuffer A= a.reverse();
System.out.println(A);
}
} 这样也可以
public static void main(String args[]) {
String str = "Hello World!";
StringBuffer a = new StringBuffer(str);
StringBuffer A = a.reverse();
System.out.println(A);
}
}
a.reverse()返回的是StringBuffer型 而不是String型
public static void main(String[] args){
String s = "Hello World!";
StringBuffer b = new StringBuffer(s);
String a =b.reverse().toString();
System.out.println(a);
}
}返回的类型是StringBuffer类型