python challenge challenge1 之java解

python challeng见 www.pythonchallenge.com
第0题就不解了,就是2的38次方
从第1题开始
第1题是密码转换
解码原则:每个字母都右移2位
public class Challenge1Decode {

	public static void main(String[] args) {
		String encodeStr = "map";
		System.out.println(decodeStr(encodeStr));
		
		String x = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.";
		String alphabet = "abcdefghijklmnopqrstuvwxyz";
		String out = "";
		for (int i = 0; i < x.length(); i++) {
			if (alphabet.indexOf(x.charAt(i)) == -1)
				out += x.charAt(i);
			else
				out += alphabet.charAt((alphabet.indexOf(x.charAt(i)) + 2) % 26);
		}
		System.out.println(out);
	}
	
	public static String decodeStr(String str){
		char[] charArr = str.toCharArray();
		StringBuilder builder = new StringBuilder();
		for(char oldChar : charArr){
			char newChar = decodeChar(oldChar);
			builder.append(newChar);
		}
		return builder.toString();
	}
	
    public static char decodeChar(char oldChar){
    	if(oldChar >= 'a' && (int)oldChar <= 'x' ){
    		return (char)(oldChar + 2);
    	}else if(oldChar == 'y'){
    		return 'a';
    	}else if(oldChar == 'z'){
    		return 'b';
    	}else{
    		return oldChar;
    	}

	}
    
    
}

你可能感兴趣的:(java,python)