zoukankan      html  css  js  c++  java
  • 课后作业四

    (一)课后作业之字串加密

    源代码:

    //算法加密,字符向后退三位

    //范亚雷 2015.10.25

    import javax.swing.JOptionPane;

    public class Encryption {

    public static void main(String[] args){

    String s=JOptionPane.showInputDialog("请输入要加密的字符串:");//输入字符串

    char charArray[]=s.toCharArray();     //将字符串转化成字符数组

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

      {

    if(charArray[i]=='x'||charArray[i]=='y'||charArray[i]=='z')//字符为xyz时加密为abc

       {charArray[i]=(char)(charArray[i]-23);}//char)返回对应于数字代码的字符。函数 CHAR 可将其他类型计算机文件中的代码转换为字符

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

      }

    JOptionPane.showMessageDialog( null,String.valueOf(charArray),"经过加密后,字符串变为:",JOptionPane.CLOSED_OPTION);

    }

    }

    运行结果截图:

    (二)动手动脑之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));

            

        

    }

    }

    Java中的equals()方法是在Object类中定义,equals 方法被用来检测两个对象是否相等,即两个对象的内容是否相等。在实际使用的过程中,由于=是检测两个对象的地址,所以我们就可以重写equals()方法比较内容是否相同。

    (三)整理String类的Length()charAt()、 getChars()replace()、 toUpperCase()、 toLowerCase()trim()toCharArray()使用说明。

    length()//求字符串长度

    使用说明:

    String s=jdbfkjdbfksdjbfksjd;

    System.out.println(s.length());

    charAt(index)//index 是字符下标,返回字符串中指定位置的字符

    使用说明:

    String s=love;

    System.out.println(s.charAt(3));

    getChars()//将字符从此字符串复制到目标字符数组

    使用说明:

    String str = "hbjhshvjh";

    Char[] ch = new char[8];

    str.getChars(2,5,ch,0);

    replace()//替换字符串

    使用说明:

    String s=abc”;

    System.out.printlns.replace(abc,xyz);

    toUpperase()//将字符串全部转换成大写

    使用说明:

    System.out.println(new String(hi).toUpperCase());

    toLowerCse()//将字符串全部转换成小写

    使用说明:

    System.out.println(new String(HI).toLowerCase());

    trim()//是去两边空格的方法

    使用说明:

    String x=ab  c;

    System.out.println(x.trim());

    toCharArray(): // 将字符串对象中的字符转换为一个字符数组

    使用说明:

    String x=abcd;

    char myChar[]=x.toCharArray();

    System.out.println(myChar[1]:”+myChar[1]);

  • 相关阅读:
    javascript中的screen对象
    javascript的navigator对象
    Javascript的location对象
    javascript 关于闭包的知识点
    微信小程序教程(第三篇)
    微信小程序----关于变量对象data 和 前端wxml取后台js变量值
    微信小程序教程(第四篇)
    微信小程序教程(第二篇)
    微信小程序教程(第一篇)
    操作文件的方法
  • 原文地址:https://www.cnblogs.com/fan-xiaofan/p/4909895.html
Copyright © 2011-2022 走看看