前鱼不泣 2022-02-25 06:42 采纳率: 81.8%
浏览 44
已结题

java 凯撒密码用rotate解决

在名为 的文件Caesar.java中,实现以下 ( public static) 方法。

一种称为rotate旋转单个字符的方法。它应该有两个参数:第一个是整数 ( int) 移位,第二个是char要旋转的 a,并返回按给定移位旋转的字符,作为 a char。

小写字符应转换为小写字符,大写字符应转换为大写字符,所有其他字符应保持不变。

另一种称为rotate旋转整个字符串的方法。它应该再次接受两个参数:第一个是整数 ( int) 移位,第二个是String要旋转的 a。它应该返回按给定班次旋转的字符串,作为String. 当然你可以在这里使用你的角色旋转方法。

一种main允许对文本进行编码/解码的方法。

偏移量是输入的

输出大概这样
$> java Caesar 3 "The ships hung in the sky in much the same way that bricks don't."
Wkh vklsv kxqj lq wkh vnb lq pxfk wkh vdph zdb wkdw eulfnv grq'w.

$> java Caesar -13 "The ships hung in the sky in much the same way that bricks don't."
Gur fuvcf uhat va gur fxl va zhpu gur fnzr jnl gung oevpxf qba'g.

$> java Caesar 13 The ships hung in the sky in much the same way that bricks don't.
Too many parameters!
Usage: java Caesar n "cipher text"

$> java Caesar 13
Too few parameters!
Usage: java Caesar n "cipher text"

  • 写回答

2条回答 默认 最新

  • nihui123 2022-02-25 07:19
    关注
    
    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条)

报告相同问题?

问题事件

  • 系统已结题 3月5日
  • 已采纳回答 2月25日
  • 请详细说明问题背景 2月25日
  • 创建了问题 2月25日

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?