weixin_45710228 2019-10-04 08:18 采纳率: 0%
浏览 375
已采纳

java 问题,有没有大神知道凯撒密码怎么做?

写一个加密和解密的method,还有用户输入的密钥,密钥 +就是顺着移动(如果+2,就是则字母A将变为C,字母B将变为D),- 就是倒着移动。
1 是加密
2 是解密
下面是给出的代码
import java.util.Scanner;

public class CaesarCypher {

// Write an encode method

// Write a decode method



public static void main(String[] args) {    

    Scanner in = new Scanner(System.in);

    String message = "";




    System.out.println("Do you have a message to (1) encode or (2) decode");
    int option = in.nextInt();
    in.nextLine();

    if (option==1){


      System.out.println("Type your message to encode");
      message = in.nextLine();


    }

    if (option==2){

      System.out.println("Type your message to decode");

      message = in.nextLine(); 

    }


    }

}
  • 写回答

2条回答 默认 最新

  • threenewbee 2019-10-04 10:05
    关注
    import java.util.Scanner;
    
    public class CaesarCypher {
    
    // Write an encode method
    
    // Write a decode method
    
        public static String Decrypt(String str, int n) {
            // TODO Auto-generated method stub
            //解密
            int k=Integer.parseInt("-"+n);
            String string="";
            for(int i=0;i<str.length();i++) {
                char c=str.charAt(i);
                if(c>='a'&&c<='z')//如果字符串中的某个字符是小写字母
                {
                    c+=k%26;//移动key%26位
                    if(c<'a')
                        c+=26;//向左超界
                    if(c>'z')
                        c-=26;//向右超界
                }else if(c>='A'&&c<='Z')//如果字符串中的某个字符是大写字母
                {
                    c+=k%26;//移动key%26位
                    if(c<'A')
                        c+=26;//向左超界
                    if(c>'Z')
                        c-=26;//向右超界
                }
                string +=c;//将解密后的字符连成字符串
            }
            return string;
        }
    
        public static String Encryption(String str, int k) {
            // TODO Auto-generated method stub
            //加密
            String string="";
            for(int i=0;i<str.length();i++) {
                char c=str.charAt(i);
                if(c>='a'&&c<='z')//如果字符串中的某个字符是小写字母
                {
                    c+=k%26;//移动key%26位
                    if(c<'a')
                        c+=26;//向左超界
                    if(c>'z')
                        c-=26;//向右超界
                }else if(c>='A'&&c<='Z')//如果字符串中的某个字符是大写字母
                {
                    c+=k%26;//移动key%26位
                    if(c<'A')
                        c+=26;//向左超界
                    if(c>'Z')
                        c-=26;//向右超界
                }
                string +=c;//将解密后的字符连成字符串
            }
            return string;
        }
    
    
    public static void main(String[] args) {    
    
        Scanner in = new Scanner(System.in);
    
        String message = "";
    
        int key = 1; //你的密钥
    
    
        System.out.println("Do you have a message to (1) encode or (2) decode");
        int option = in.nextInt();
        in.nextLine();
    
        if (option==1){
    
    
          System.out.println("Type your message to encode");
          message = in.nextLine();
        String r = Encryption(message, key);
            System.out.println(r);
    
        }
    
        if (option==2){
    
          System.out.println("Type your message to decode");
    
          message = in.nextLine(); 
            String r = Decrypt(message, key);
            System.out.println(r);
    
        }
    
    
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器