String课后作业

请查看String.equals()方法的实现代码,注意学习其实现方法。

public class StringEquals {

       @param args the command line arguments
     
       public static void main(String[] args) {
        
         String s1=new String("Hello");
         
         String s2=new String("Hello");
       
         System.out.println(s1==s2);
        
       System.out.println(s1.equals(s2));
       
         String s3="Hello";
         
        String s4="Hello";

            System.out.println(s3==s4);
        
            System.out.println(s3.equals(s4));
       
     }
 
}

String类中的equals()方法,是用来判断当前字符串与传递进来的字符串的内容是否一致。String.equals()方法比较的是字符串的内容,使用equals(...)方法会对字符串中的所有字符一个接一个地进行比较,如果完全相等那么返回。
字串加密

    古罗马皇帝凯撒在打仗时曾经使用过加密军事情报: 请编写一个程序,使用上述算法加密或解密用户输入的英文字串。

    源程序:

import javax.swing.JOptionPane;

public class Jiami{

    public static void main(String[] args) {

     String yuanwen;

     yuanwen =JOptionPane.showInputDialog( "请输英文字串" );

    char chs[]=yuanwen.toCharArray();

     for(int i=0;i<yuanwen.length();i++)

    {

       if(chs[i]=='X'||chs[i]=='Y'||chs[i]=='Z')

    {

    chs[i]=(char)(chs[i]-23);

  }

     else

   {

      chs[i]=(char)(chs[i]+3);

     }

}

JOptionPane.showMessageDialog(null,"加密后的字串为:"+String.valueOf(chs));

   }

}

结果截图:


   

 

原文地址:https://www.cnblogs.com/liulitianxia/p/4909407.html