1. 程式人生 > >用JAVA寫一個簡單的英文加密器

用JAVA寫一個簡單的英文加密器

package qhs;

import java.util.Scanner;

public class JiaM {

	public static void main(String[] args) {

		String[] A = new String[5000];

		String min;
		String mi;
		//String sf;
		int cs;
		String Q="";

		Scanner s = new Scanner(System.in);
		
		//System.out.println("加密請輸入 'y',解密請輸入'n'");
		//sf = s.nextLine();
		
		System.out.println("請輸入要加密的字元(英文)");
		min = s.nextLine();
		
		System.out.println("請輸入要加密的次數");
		cs = s.nextInt();

		//往數組裡放元素
		for (int i = 0; i < min.length(); i++) {
			char B = min.charAt(i);

			//System.out.println(B);
			A[i] = B + "";
		}


		for(int q=0;q<=cs;q++) {      //外迴圈控制重複加密的次數
			System.out.print("第"+q+"次");
			for (int x = 0; x < min.length(); x++) {  //內迴圈進行逐字元加密
				switch (A[x]) {            //加密演算法
					case " ":
						A[x] = " ";
						break;
						
					case ",":
						A[x] = ",";
						break;
						
					case ".":
						A[x] = ".";
						break;
						
					case "a":
						A[x] = "d";
						break;
						
					case "b":
						A[x] = "f";
						break;
						
					case "c":
						A[x] = "h";
						break;
						
					case "d":
						A[x] = "j";
						break;
						
					case "e":
						A[x] = "l";
						break;
						
					case "f":
						A[x] = "n";
						break;
						
					case "g":
						A[x] = "p";
						break;
						
					case "h":
						A[x] = "r";
						break;
						
					case "i":
						A[x] = "t";
						break;
						
					case "j":
						A[x] = "v";
						break;
						
					case "k":
						A[x] = "x";
						break;
						
					case "l":
						A[x] = "z";
						break;
						
					case "m":
						A[x] = "b";
						break;
						
					case "n":
						A[x] = "e";
						break;
						
					case "o":
						A[x] = "g";
						break;
						
					case "p":
						A[x] = "i";
						break;
						
					case "q":
						A[x] = "k";
						break;
						
					case "r":
						A[x] = "m";
						break;
						
					case "s":
						A[x] = "o";
						break;
						
					case "t":
						A[x] = "q";
						break;
						
					case "u":
						A[x] = "s";
						break;
						
					case "v":
						A[x] = "w";
						break;
						
					case "w":
						A[x] = "u";
						break;
						
					case "x":
						A[x] = "y";
						break;
						
					case "y":
						A[x] = "a";
						break;
						
					case "z":
						A[x] = "c";
						break;
				}
			}
			for (int x = 0; x < min.length(); x++) {
				System.out.print(A[x]);
				
			}
			System.out.println("");
			
		}


	}

}