import java.io.*;
public class TestStringBuffer01
{
public static void main(String args[])throws IOException
{
BufferedReader buf;
String s1;
buf =new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入字符串:");
s1=buf.readLine();
StringBuffer sb1=new StringBuffer(s1);
StringBuffer sb2=sb1.reverse();
//System.out.println(sb1);
//System.out.println(sb2);
System.out.print("s1是否为回文:"+s1.equals(sb2.toString()));//这里的比较,sb2为什么要加toString啊?因为它是StringBuilder类型吗?
}
}
不是很懂哎!!!求指点......
public class TestStringBuffer01
{
public static void main(String args[])throws IOException
{
BufferedReader buf;
String s1;
buf =new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入字符串:");
s1=buf.readLine();
StringBuffer sb1=new StringBuffer(s1);
StringBuffer sb2=sb1.reverse();
//System.out.println(sb1);
//System.out.println(sb2);
System.out.print("s1是否为回文:"+s1.equals(sb2.toString()));//这里的比较,sb2为什么要加toString啊?因为它是StringBuilder类型吗?
}
}
不是很懂哎!!!求指点......
解决方案 »
- 100 分 java SQL语句拼接问题
- 怎样实现在图形界面中选择文件?
- 请教格式转换
- 关于java访问sqlserver的问题
- !!!很急!!!帮忙写个应用!分多少都行,谢谢。
- 请各位前辈帮帮我吧。
- [调查] 用 JAVA 开发 ERP 软件的情况 !
- Java 线程不能notify
- scjp的培训参考书谁有?请联系:[email protected],指定参考书是哪个出版社的?
- 关于java Class类的疑惑
- FileInputStream的read(byte[] b) 问题。。
- BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) 方法
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
也就是说:将此字符串与指定的对象比较。当且仅当该参数不为 null,并且是与此对象表示相同字符序列的 String 对象时,结果才为 true。
StringBuilder与String类型都不同,比较的话肯定返回false,所以要先把StringBuilder.toString()转换为String类型比较才有意思!