简体   繁体   English

Java中的Caesar密码

[英]Caesar cipher in Java

I'm trying to implement the Caesar cipher . 我正在尝试实施凯撒密码

But while doing so I am getting an unexpected output which I am unable to rectify. 但是这样做时,我得到了无法纠正的意外输出。

Will someone help me please? 有人可以帮我吗?

My code is as follows: 我的代码如下:

import java.io.*;

public class encryptology
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int t, move = Integer.parseInt(br.readLine());
        String s = "", st = br.readLine();
        int l = st.length();

        for(int x = 0; x < l; x++){
            char c = st.charAt(x);
            t = (int)c;
            if(move != 0){
                t = t + move;
                if(t > 90){
                    t = t - 26;        
                }
                if(t < 65){
                    t += 26;
                }
                c = (char)t;
                s = st + c;
            }
        }
        System.out.println(s);
    }
}

I entered move = 2 and st = charles 我输入move = 2st = charles
The output was: charles[ 输出为: charles[

You are changing wrong string. 您正在更改错误的字符串。 You are acctualy setting result with starting string st and add encrypted char. 您正在以起始字符串st灵活设置结果并添加加密的char。 Change 更改

s=st+c;

to

s=s+c;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM