【问题标题】:Keep Spaces, Punctuation, and char Cases in Java Caesar Cipher在 Java Caesar Cipher 中保留空格、标点符号和字符大小写
【发布时间】:2016-01-06 11:12:30
【问题描述】:

我正在尝试加密一个 txt 文件,但是当我将我的字符发送到数组时,我丢失了我的空格。我想保留我的空格以及标点符号和字母大小写。我是如此接近,但似乎无法做任何不使 A.)所有内容为空字符或 B.)循环大写字母的事情。提前致谢。

public class Encryption {
     CaesarCipher c= new CaesarCipher();
     Scanner kb = new Scanner(System.in);
     String end = "";

public void changeLetters(File file) {
    System.out.println("How far would you like to shift?");
    int shift = Integer.parseInt(kb.nextLine());
    Scanner fileScanner;
    try {
        fileScanner = new Scanner(file);
        while (fileScanner.hasNextLine()) {
            String line = fileScanner.nextLine();
            shift(line, shift);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

private void shift(String line, int shift) {
    char[] og = line.toCharArray();
    for (int i = 0; i < og.length; i++) {
        char letter = og[i];
        letter = (char) (letter + shift);
        if (letter > 'z') {
            letter = (char) (letter - 26);
        } else if (letter < 'a') {
            letter = (char) (letter + 26);
        }
        end = end + Character.toString(letter);
    }
    System.out.println(end);

    File file = new File("Encrypted.txt");
    FileWriter writer = null;

    {
        try {
            writer = new FileWriter(file);
            writer.write(end);
            writer.close();
        } catch (

        IOException e)

        {
            e.printStackTrace();
        }
        System.out.println("Decryption Complete");
        System.out.println("Q to quit, C to Continue");
        String response = kb.next();
        if (response.equals("q") || response.equals("Q")) {
            System.out.println("Goodbye");
        } else if (response.equals("c") || response.equals("C")) {
            c.getInformation();
        }
    }
}

【问题讨论】:

    标签: java arrays char uppercase caesar-cipher


    【解决方案1】:

    我认为问题出在您在信中添加 (+/-) 26 的事实,例如 letter = (char) (letter - 26);。这仅适用于字母表 [a-z]。但是,由于您希望能够处理大写字母、特殊字符等,因此您不能这样做。

    为了做到这一点,使用模运算符% 也会更简洁。因此,您不必像 if (letter &gt; 'z') 那样进行显式测试。

    这是shift的过程,真的很简单

    private String shift(String str, int shift) {
        String shifted = "";
        for(int i = 0; i < str.length(); i++) {
            char original = str.charAt(i);
            char shiftedChar = (char) ((original + shift) % Integer.MAX_VALUE);
            shifted += shiftedChar; // Append shifted character to the end of the string
        }
    
        return shifted;
    }
    

    但是我不确定这是要使用的模数。但我做了一些测试,这似乎有效。

    这里是你如何换档和取消换档

    String test = "This is a test!";
    String encoded = shift(test, 3);
    String decoded = shift(encoded, -3);
    
    System.out.println("Encoded : " + encoded + "\n" + "Decoded : " + decoded);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 2023-03-09
      • 2011-04-08
      • 2019-06-27
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      相关资源
      最近更新 更多