----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------
/*要求:参考API帮助,查找相关的方法,使用String类完成如下功能,对英文字符串进行加密处理。
(1) 将给定的英文字符取相反顺序,并改变每个字符的大小写形式。
(2) 将经第一步处理的信息进一步加工,将每个字符取其所在字母表中的顺序,取其后一个字母。
(3) 编写解密方法。
(4) 编写测试函数,在运行时通过命令行参数接受需要处理的字符串,将源字符串、加密字符串和解密字符串打印到屏幕输出。
*/
import java.lang.*;
import java.util.*;
import java.io.*;
class StringProc
{
public static void main(String[] args)
{
String str = "";
System.out.println("请输入英文字符串");
str = inputStr();//从键盘输入字符串
System.out.println("加密后输出");
Encryption e = new Encryption(str);
System.out.println(e.encryption(e.get()));
System.out.println("解密后输出");
Deciphering d = new Deciphering(e.encryption(e.get()));
System.out.println(d.deciphering(e.get()));
}
public static String inputStr()
{
BufferedReader bufr = null;
BufferedWriter bufw = null;
String line = null;
try
{
bufr = new BufferedReader(new InputStreamReader(System.in));
bufw = new BufferedWriter(new OutputStreamWriter(System.out));
//String line = null;
if((line=bufr.readLine())!=null)
{
bufw.write(line,0,line.length());
bufw.newLine();
bufw.flush();
}
return line;
}
catch (IOException e)
{
throw new RuntimeException("读写失败");
}
finally
{
/*
try
{
if(bufr!=null)
bufr.close();
}
catch (IOException e)
{
throw new RuntimeException("读取流关闭失败");
}
try
{
if(bufw!=null)
bufw.close();
}
catch (IOException e)
{
throw new RuntimeException("写入流关闭失败");
}
*/
}
}
}
//加密类
class Encryption
{
private String str = null;
//构造方法
Encryption(String str)
{
this.str = str;
}
public String get()
{
return this.str;
}
//加密方法
public String encryption(String str)
{
String oldString = str;
String newString = null;
//改变每个字符的大小写形式并将字符串取反
return newString = this.upperLowerCaseReverse(this.reverse(oldString));
}
//交换大小写并返回处理后的字符串
private String upperLowerCaseReverse(String oldString)
{
String str = oldString;
char[] charArray = str.toCharArray();
for(int i=0;i<charArray.length;i++)
{
char ch = charArray[i];
if(ch=='z')
charArray[i] = 'A';
else if(ch=='Z')
charArray[i] = 'A';
else if(Character.isLowerCase(ch)==true)
charArray[i] = (char)(Character.toUpperCase(ch) + 1);//小写英文字符改为大写取其所在字母表中的顺序其后一个字母
else if(Character.isUpperCase(ch)==true)
charArray[i] = (char)(Character.toLowerCase(ch) + 1);//大写英文字符改为小写取其所在字母表中的顺序其后一个字母
else
continue;
}
return String.copyValueOf(charArray);
}
//将给定的英文字符取相反顺序
private String reverse(String str)
{
StringBuilder sb = new StringBuilder(str);
return sb.reverse().substring(0,sb.length());
}
}
class Deciphering //解密类
{
private String str = null;
//构造方法
Deciphering(String str)
{
this.str = str;
}
public String get()
{
return this.str;
}
public String deciphering(String str)
{
String oldString = str;
String newString = null;
//改变每个字符的大小写形式并将字符串取反
return newString = this.upperLowerCaseReverse(this.reverse(oldString));
}
//交换大小写并返回处理后的字符串
private String upperLowerCaseReverse(String oldString)
{
String str = oldString;
char[] charArray = str.toCharArray();
for(int i=0;i<charArray.length;i++)
{
char ch = charArray[i];
if(ch=='a')
charArray[i] = 'Z';
else if(ch=='z')
charArray[i] = 'A';
else if(Character.isLowerCase(ch)==true)
charArray[i] = (char)(Character.toUpperCase(ch) - 1);//小写英文字符改为大写取其所在字母表中的顺序前面一个字母
else if(Character.isUpperCase(ch)==true)
charArray[i] = (char)(Character.toLowerCase(ch) - 1);//大写英文字符改为小写取其所在字母表中的顺序前面一个字母
else
continue;
}
return String.copyValueOf(charArray);
}
//将给定的英文字符取相反顺序
private String reverse(String str)
{
StringBuilder sb = new StringBuilder(str);
return sb.reverse().substring(0,sb.length());
}
}
----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------