java动手动脑 3

1.String.equals()方法的实现代码

作用;

   用于判断两个变量是否是对同一个对象的引用,即堆中的内容是否相同,返回值为布尔类型 

方法;

boolean b=obj1.equals(obj2);

String类型比较不同对象内容是否相同,应该用equals,因为==用于比较引用类型和比较基本数据类型时具有不同的功能。

对象不同,内容相同,"=="返回falseequals返回true

代码;

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)

}

}

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

Length():求字符串长度

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

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

replace():替换字符串

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

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

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

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

 

 

3.加密

设计思想;

    先输入一个字符串,将其拆分成字符然后在对每个字符进行加密,在将字符连接成字符串,输出。

流程图;

源代码;package jiami;

import javax.swing.JOptionPane;

public class jiami {

    public static void main(String[] args) {

           String input=JOptionPane.showInputDialog("请输入要加密的英文字符串");

           

           int y;

           y=input.length();

           String s1=new String();

           String s2=new String();

           char c;

           int i;

            for(int x=0;x<y;x++)

            {

            s1=input.substring(x, x+1);

           c=s1.charAt(0);

            i=c+3;

            c=(char)i;

            s2+=c;

            }

            String output="结果是"+s2;

           JOptionPane.showConfirmDialog(null,output);

    }

}

结果截图;

原文地址:https://www.cnblogs.com/1336303471-tengxianliang/p/4907252.html