有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我需要chiper实验室的帮助。我的说明如下:

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 alphabet = "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);
  }  
 }

共 (2) 个答案

  1. # 1 楼答案

    试试这个:

    public class CaesarCipher {
    
        private static final int A = (int)'A';
        private static final int Z = (int)'Z';
    
        public static void main(String[] args) {
            for (String s : args)
                System.out.println(new Encoder(s));
        }
    
        private static class Encoder {
            private String encoded;
            public Encoder(String s) {
                s = s.toUpperCase();
                StringBuilder sb = new StringBuilder();
                for (char c : s.toCharArray())
                    sb.append((char)(A + Z - (int)c));
                encoded = sb.toString();
            }
    
            @Override
            public String toString() { return encoded; }
        }
    
    }
    
  2. # 2 楼答案

    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
        }
    }
    

    您可以选择在构造函数、print方法或其他方式中进行加密