gpt4 book ai didi

java - 无法让我的密码实验室工作

转载 作者:行者123 更新时间:2023-12-01 05:08:35 25 4
gpt4 key购买 nike

我的削片机实验室需要帮助。我的指示是:<​​/p>

Write a program that accepts any number of strings as command-line arguments and displays those strings encrypted with the Atbash cipher. You program should be as modular as possible and use good object oriented programming techniques. Your program must be thoroughly documented using javadoc comments.

我有String letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";,它应该对字符串进行编码,以便A返回Z,B返回Y等等。我在 Eclipse 中完成了我的密码实验室,但它没有运行。我不确定我做错了什么。

public class CaesarCipher {
public static void main(String[] args) {
CaesarCipher cc = new CaesarCipher();
}

public static final int ALPHASIZE = 26;
public static final char [] alpha = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
protected char[] encrypt = new char[ALPHASIZE];
protected char[] decrypt = new char[ALPHASIZE];

public CaesarCipher() {
for (int i=0; i<ALPHASIZE; i++)
encrypt[i] = alpha[(i + 3) % ALPHASIZE];
for (int i=0; i<ALPHASIZE; i++)
decrypt[encrypt[i] - 'A'] = alpha[i];
}

/** Encryption Method */
public String encrypt(String secret) {
char[] mess = secret.toCharArray();
for (int i=0; i<mess.length; i++)
if (Character.isUpperCase(mess[i]))
mess[i] = encrypt[mess[i] - 'A'];
return new String(mess);
}

/** Decryption Method */
public String decrypt(String secret) {
char[] mess = secret.toCharArray();
for (int i=0; i<mess.length; i++)
if (Character.isUpperCase(mess[i]))
mess[i] = decrypt[mess[i] - 'A'];
return new String(mess);
}
}

最佳答案

您的 main 方法所做的就是调用 main 方法所在类的构造函数。

这是非常令人困惑的代码,而且形式也不是很好。

您可能想要做的是将大部分代码包含在主方法中。您可以通过将代码从那里组织到其他类中来使用“使用良好的面向对象编程技术”。

我会做类似的事情

public class CaesarCipher 
{
public static void main(String[] args)
{
for(int i=0; i<args.length; i++)
{
Cypher cypher = new Cypher(args[i]);
System.Out.Println(cypher.Print());
}
}
}

然后在另一个文件中(或者同一个文件也可以)

public class Cypher
{
// fields to represent your cypher

public Cypher(String s)
{
//load the input string into your cypher here.
}

public String Print()
{
//print the encrypted string
}
}

您可以选择在构造函数、打印方法或其他方式中加密。

关于java - 无法让我的密码实验室工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12377292/

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