----------- 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學習型技術博客、期待與您交流! ------------