我尝试用 Java 制作带有分隔符输入的凯撒密码。

huangapple go评论83阅读模式
英文:

I tried to make caesar cipher with delimiter input in java

问题

<!-- language: lang-java -->

    public static void main(String[] args) {
        String message, encryptedMessage = "";
        int key;
        char ch;
        Scanner sc = new Scanner(System.in);

        key = sc.nextInt();

        message = sc.nextLine();
        sc.useDelimiter(" ");

        // 我认为问题出在加密公式上。

        for(int i = 0; i < message.length(); ++i){
            ch = message.charAt(i);

            if(ch >= 'a' && ch <= 'z'){
                ch = (char)(ch + key);

                if(ch > 'z'){
                    ch = (char)(ch - 'z' + 'a' - 1);
                }

                encryptedMessage += ch;
            }
            else if(ch >= 'A' && ch <= 'Z'){
                ch = (char)(ch + key);

                if(ch > 'Z'){
                    ch = (char)(ch - 'Z' + 'A' - 1);
                }

                encryptedMessage += ch;
            }
            else {
                encryptedMessage += ch;
            }
        }

        while(sc.hasNext()){
            System.out.println(sc.next() + encryptedMessage);
        }
        sc.close();
    }
英文:

<!-- language: lang-java -->

public static void main(String[] args) {
String message, encryptedMessage = &quot;&quot;;
int key;
char ch;
Scanner sc = new Scanner(System.in);
key = sc.nextInt();
message = sc.nextLine();
sc.useDelimiter(&quot; &quot;);
// I think the problem is in the encrypting formula.
for(int i = 0; i &lt; message.length(); ++i){
ch = message.charAt(i);
if(ch &gt;= &#39;a&#39; &amp;&amp; ch &lt;= &#39;z&#39;){
ch = (char)(ch + key);
if(ch &gt; &#39;z&#39;){
ch = (char)(ch - &#39;z&#39; + &#39;a&#39; - 1);
}
encryptedMessage += ch;
}
else if(ch &gt;= &#39;A&#39; &amp;&amp; ch &lt;= &#39;Z&#39;){
ch = (char)(ch + key);
if(ch &gt; &#39;Z&#39;){
ch = (char)(ch - &#39;Z&#39; + &#39;A&#39; - 1);
}
encryptedMessage += ch;
}
else {
encryptedMessage += ch;
}
}
while(sc.hasNext()){
System.out.println(sc.next()+encryptedMessage);
}
sc.close();
}

This is my code and I use delimiter for the text. The input I use doesn't get encrypted. I don't understand what the problem is. Can somebody help me with the code?

答案1

得分: 0

你的问题出在接受 [encryption] 密钥和要加密的消息的方式上。根据你问题中的代码,你需要先输入一个整数,然后紧接着输入消息。例如,

<!-- language: lang-none -->
1Abc xyz.

在上面的例子中,密钥是 1,消息是 Abc xyz. 你在输入消息的最后一个字母后按下回车键,你的代码会正确生成加密消息,根据我上面的例子,加密后的消息是

<!-- language: lang-none -->
Bcd yza.

如果在输入 [encryption] 密钥后(在我的例子中是 1),然后再按下回车键,则“消息”会是一个空字符串。这就是为什么你的消息似乎没有被加密。

如果你使用调试器逐步执行代码,你会发现“消息”是一个空字符串。所有好的集成开发环境都配备了调试器。你应该学会如何使用它。

也许 Scanner 在使用 next() 或 nextFoo() 后跳过 nextLine() 问题 可以帮助你理解 Scanner 类的工作原理。

顺便说一下,你不应该关闭包装了 System.inScanner。你可以安全地忽略你的集成开发环境可能显示的有关不要关闭 Scanner 的警告。另外,你最后的 while 循环是不必要的。直接打印 encryptedMessage,即

System.out.println(encryptedMessage);
英文:

Your problem is with the way you are accepting the [encryption] key and the message to encrypt. Using the code in your question, you need to enter an integer immediately followed by the message, for example.

<!-- language: lang-none -->

1Abc xyz.

In the above example, the key is 1 (one) and the message is Abc xyz. You hit the &lt;ENTER&gt; key after entering the the last letter in the message and your code correctly generates the encrypted message which, according to my example above, is

<!-- language: lang-none -->

Bcd yza.

If you hit &lt;ENTER&gt; after entering the [encryption] key (which is 1 in my example, above) then the "message" is an empty string. That's why it appears that your message is not encrypted.

If you step through your code with a debugger, then you would discover that "message" is an empty string. All good IDEs have a debugger. You should learn to use it.

Perhaps Scanner is skipping nextLine() after using next() or nextFoo()? will help you understand how Scanner class works.

By the way, you should not close a Scanner that wraps System.in. You can safely ignore any warnings your IDE may display about not closing the Scanner. Also, your last while loop is not required. Simply print encryptedMessage, i.e.

System.out.println(encryptedMessage);

huangapple
  • 本文由 发表于 2020年10月18日 20:34:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64413355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定