课后作业01:
【程序设计思想】
想要实现两种操作原理都很简单,加密,即将每一个字符都进行+3操作,解密,即把每一个字符都进行-3操作。再讲每个字符依次输出即可!
【程序流程图】
【源代码】
//信1605-2班 20163448 那颖
import java.util.Scanner;
public class CaesarCipher
{
public static void main(String[] args)
{
System.out.println("Please choose one or two:");
System.out.println("1.加密操作");
System.out.println("2.解密操作");
int x;
Scanner input=new Scanner(System.in); //选择要进行的操作
x=input.nextInt();
//实现加密操作
if(x==1)
{
System.out.println("请输入要加密的字符:");
String s;
Scanner input1=new Scanner(System.in);
s=input1.nextLine();
System.out.println("加密后的字符为:");
for(int i=0;i<3;i++)
{
char c;
c=(char) (s.charAt(i)+3);
System.out.print(c+" ");
}
}
//实现解密操作
else if(x==2)
{
System.out.println("请输入要加密的字符:");
String s;
Scanner input2=new Scanner(System.in);
s=input2.nextLine();
System.out.println("加密后的字符为:");
for(int i=0;i<3;i++)
{
char c;
c=(char) (s.charAt(i)-3);
System.out.print(c+" ");
}
}
else
{
System.out.println("输入错误,请重新输入!");
}
}
}
【结果截图】
课后作业02:
1、【动手动脑】
String.equals()方法
public class StringEquals {
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():
参数:无
返回值:调用此方法的字符串的长度(int)
charAt() :
方法用于返回指定索引处的字符,索引范围为从 0 到 length() - 1。
返回值:返回指定索引处的字符
getChars():
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin):
将字符从此字符串复制到目标字符数组。
参数:
srcBegin -- 字符串中要复制的第一个字符的索引。
srcEnd -- 字符串中要复制的最后一个字符之后的索引。
dst -- 目标数组。
dstBegin -- 目标数组中的起始偏移量。
replace():
String replace(char oldChar, char newChar)
replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。
参数:
oldChar -- 原字符。
newChar -- 新字符。
返回值:替换后生成的新字符串。
toUpperCase():
String toUpperCase()
toUpperCase() 方法将字符串小写字符转换为大写。
参数:无
返回值:字符转换为大写后的字符串。
toLowerCase():
String toLowerCase()
toLowerCase() 方法将字符串转换为小写。
参数:无
返回值:字符转换为小写后的字符串。
trim():
String trim():
trim() 方法用于删除字符串的头尾空白符。
参数:无
返回值:删除头尾空白符的字符串。
toCharArray():
char[] toCharArray():
toCharArray() 方法将字符串转换为字符数组。
参数:无
返回值:字符数组