gpt4 book ai didi

java - 尝试学习 "one letter off"加密技术

转载 作者:行者123 更新时间:2023-12-02 01:51:36 25 4
gpt4 key购买 nike

这是作业的一小部分,我必须扫描整个文本文件并加密/解密整个文件,但我认为在尝试使用多个文本文件进行此操作之前,我应该能够理解基础知识字。

我试图弄清楚为什么我的代码只更改我尝试加密的单词的最后一个字母,而不是整个单词的每个字母。

代码:

import java.util.*;

public class Encryptor
{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
String word = "";
char ch = ' ';

System.out.println("What word do you want to encrypt");
word = in.nextLine();

//Encrypt word
for(int i = 0; i<word.length();i++)
{
ch = word.charAt(i);
ch++;
}

System.out.println(ch);


}

}

最佳答案

您将 word.charAt(i); 分配给 ch,然后递增 ch。然而,这不会改变word的值。为此,您可以执行以下操作:

Scanner in = new Scanner(System.in);
String word = "";
StringBuilder encrypedWordSB = new StringBuilder();

System.out.println("What word do you want to encrypt");
word = in.nextLine();

// Encrypt word
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
ch++;

encrypedWordSB.append(ch);
}

System.out.println(encrypedWordSB);

在上面的示例中,我们对 for 循环内的字符 ch 进行加密,然后将加密的字符附加到字符串中。在 for 循环之后,我们得到了包含所有加密字符的字符串 encryptedWord

关于java - 尝试学习 "one letter off"加密技术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52910856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com