Try   HackMD

簡單密碼學實作


凱薩密碼

java

import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int move=sc.nextInt(); String plaintext=sc.nextLine(); String ciphertext= plaintext; char[] ciphertextChars = ciphertext.toCharArray(); for(int i=0;i<plaintext.length();i++) { if(plaintext.charAt(i)==' ') ciphertextChars[i] = ' '; else if(plaintext.charAt(i)<'a') { ciphertextChars[i] = (char)(plaintext.charAt(i)+move); if((plaintext.charAt(i)+move)>'Z') ciphertextChars[i] = (char)(plaintext.charAt(i)+move-26); } else { ciphertextChars[i] = (char)(plaintext.charAt(i)+move); if((plaintext.charAt(i)+move)>'z') ciphertextChars[i] = (char)(plaintext.charAt(i)+move-26); } } ciphertext = String.valueOf(ciphertextChars); System.out.print(ciphertext); sc.close(); } }