使用
indexOf效率不是很高…您可以对
char值进行整数运算以获得其索引。
我在代码中添加了注释以解释更多信息,但这就是我想出的。
public class CaesarCipher { // Rotate a character k-positions public static char cipher(char c, int k) { // declare some helping constants final int alphaLength = 26; final char asciiShift = Character.isUpperCase(c) ? 'A' : 'a'; final int cipherShift = k % alphaLength; // shift down to 0..25 for a..z char shifted = (char) (c - asciiShift); // rotate the letter and handle "wrap-around" for negatives and value >= 26 shifted = (char) ((shifted + cipherShift + alphaLength) % alphaLength); // shift back up to english characters return (char) (shifted + asciiShift); } // Rotate a string k-positions public static String cipher(String s, int k) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { sb.append(cipher(s.charAt(i), k)); } return sb.toString(); } public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String password; int key; System.out.print("Please enter a password: "); password = keyboard.nextLine(); do { System.out.print("Please enter a key between 1-25: "); key = keyboard.nextInt(); if (key < 1 || key > 25) { System.out.printf(" The key must be between 1 and 25, you entered %d.n", key); } } while (key < 1 || key > 25); System.out.println("Password:t" + password); String encryption = cipher(password, key); System.out.println("Encrypted:t" + encryption); System.out.println("Decrypted:t" + cipher(encryption, -key)); }}
输出应该是这样的
Please enter a password: ABCDEFGHIJKLMNOPQRSTUVWXYZPlease enter a key between 1-25: 1Password: ABCDEFGHIJKLMNOPQRSTUVWXYZEncrypted: BCDEFGHIJKLMNOPQRSTUVWXYZADecrypted: ABCDEFGHIJKLMNOPQRSTUVWXYZ
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)