public class String用法 
{
public static void main(String args[])
{
String s1=new String("we are students");
String s2=new String("we are students");
String s3=new String("How are you");
System.out.println("S1字符的长度为:"+s1.length());
int num=0;
if(s1.equals(s2))
System.out.println("S1字符串与S2字符串相等");
if(s1.equals(s3))
System.out.println("S1字符串与S3字符串不相等");

if(s1.startsWith("we are"))
{
System.out.println("S1字符串是以we are开始");
}
if(s1.startsWith("abcde"))
{
System.out.println("S1不是以:abcde开始的");
}
for(int k=0;k<s1.length();k++)
{
if(s1.regionMatches(k, "en", 0, 2))
{
num++;
}
}
System.out.println("S1字符串中含有en的个数为:"+num);


}
if(s1.equals(s3))
   System.out.println("S1字符串与S3字符串不相等");
这里 有点问题 如何能叫他输出啊 
这里if里面是false 好像他直接就跳出了不执行了 但是我想要他输出后面那个语句 谁能帮我解决这个小问题?谢谢哈。

解决方案 »

  1.   

       if(!s1.equals(s3))
                System.out.println("S1字符串与S3字符串不相等");
      

  2.   

    if(s1.equals(s3))
                System.out.println("S1字符串与S3字符串不相等");
    这是一句相当于
    if(s1.equals(s3))
    {
         System.out.println("S1字符串与S3字符串不相等");
    }是这个样子的。
    所以当里面条件是false 的时候直接跳出if 不执行
    System.out.println("S1字符串与S3字符串不相等");
    这么改
    if(!s1.equals(s3))
                System.out.println("S1字符串与S3字符串不相等");
    或者加个else
    if(s1.equals(s3))
    {
                System.out.println("S1字符串与S3字符串相等");
    }else System.out.println("S1字符串与S3字符串不相等");