字符串 - 课后作业
一 . 字串加密:
实验题目:古罗马皇帝凯撒在打仗时曾经使用过以下方法加密军事情报,请编写一个程序,加密或解密用户输入的英文字串
设计思想:
(1)首先定义一个字符串,取出单个字符,每个字符前移三位,后移三位。(注意在加密时XYZ的变化和解密时ABC的变化)
(2)创建字符串对象s,将新得到的字符加在s后面。
(3)输出s。
可通过设置两个选项提示用户进行解密还是加密
流程图:
源代码:
import java.util.Scanner; public class Mima { public static void main(String[] args) { System.out.println("请选择:"); System.out.println("1.加密"); System.out.println("2.解密");//设置选项提示用户进行什么操作 Scanner input=new Scanner(System.in); int c=input.nextInt();//输入选项 if(c==1) { System.out.println("请输入要加密的语句:"); String str=input.next();//将输入的字符串存到str中 char[] a=new char[str.length()]; a=str.toCharArray(); for(int i=0;i<str.length();i++)//将X、Y、Z转化为A、B、C { if(a[i]=='X') a[i]='A'; else if(a[i]=='Y') a[i]='B'; else if(a[i]=='Z') a[i]='C'; else if(a[i]=='x') a[i]='a'; else if(a[i]=='y') a[i]='b'; else if(a[i]=='z') a[i]='c'; else { a[i]=(char)(a[i]+3);//将一般的数组字符直接加3然后强转为char } } String s=""; for(int i=0;i<str.length();i++)//将字符数组转化为字符串 { s=s+a[i]; } System.out.println("加密后的语句为:"+s);//输出语句 } else if(c==2) { System.out.println("请输入要解密的语句:"); String str=input.next();//将输入的字符串存到str中 char[] a=new char[str.length()]; a=str.toCharArray(); for(int i=0;i<str.length();i++)//将A、B、C转化为X、Y、Z { if(a[i]=='A') a[i]='X'; else if(a[i]=='B') a[i]='Y'; else if(a[i]=='C') a[i]='Z'; else if(a[i]=='a') a[i]='x'; else if(a[i]=='b') a[i]='y'; else if(a[i]=='c') a[i]='z'; else { a[i]=(char)(a[i]-3);//将一般的数组字符直接减3然后强转为char } } String s=""; for(int i=0;i<str.length();i++)//将字符数组转化为字符串 { s=s+a[i]; } System.out.println("解密后的语句为:"+s);//输出语句 } else { System.out.println("ERROR!"); } } }
运行截图:
二 . 动手动脑问题
1 . 请运行以下示例代码,查看其输出结果。如何解释这样的输出结果?从中你能总结出什么?
分别输出true true false
总结:在Java中,内容相同的字串常量(“Hello”)只保存一份以节约内存,所以s0,s1,s2实际上引用的是同一个对象。编译器在编译s2一句时,会去掉“+”号,直接把两个字串连接起来得一个字串(“Hello”)。这种优化工作由Java编译器自动完成。 所以前两个输出为true。当直接使用new关键字创建字符串对象时,虽然值一致(都是“Hello”),但仍然是两个独立的对象。所以最后一个输出为false。
2 .查看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)); } }
结果截图:
3 . String类的方法可以连续调用:
String str="abc";
String result=str.trim().toUpperCase().concat("defg");
请阅读JDK中String类上述方法的源码,模仿其编程方式,编写一个MyCounter类,它的方法也支持上述的“级联”调用特性,其调用示例为:
MyCounter counter1=new MyCounter(1);
MyCounter counter2=counter1.increase(100).decrease(2).increase(3);
编写类MyCounter:
public class MyCounter {public static void main(String[] args) { String s="abc"; String result=s.trim().toUpperCase().concat("xyz"); System.out.println(result); } }
4 . 整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、toLowerCase()、trim()、toCharArray()使用说明
Length():获取字串长度
例:String str =”abc”;
System.out.println(str.length());
charAt():获取指定位置的字符
例:String str=”abc”;
System.out.println(str.charAt(0));
getChars():获取从指定位置起的子串复制到字符数组中
例: String s= "abc";
Char[] ch = new char[8];
str.getChars(2,3,ch,0);
replace():子串替换
例: String s=”abc”;
System.out.println(s.replace(“abc”,”xyz”));
toUpperCase()、 toLowerCase():大小写转换
例:System.out.println(new String(“hello”).toUpperCase());
System.out.println(new String(“HELLO”).toLowerCase());
trim():去除头尾空格
例:String x=” a bc ”;
System.out.println(x.trim());
toCharArray():将字符串对象转换为字符数组
例:char myChar[]=x.toCharArray();
System.out.println(“myChar[1]”+myChar[1]);