java - 如何使密文字符与明文字符相同?

标签 java encoding rot13

package edu.secretcode;

import java.util.Scanner;

/**
 * Creates the secret code class.
 * 
 * @author
 * 
 */
public class SecretCode {
    /**
     * Perform the ROT13 operation
     * 
     * @param plainText
     *            the text to encode
     * @return the rot13'd encoding of plainText
     */

    public static String rotate13(String plainText) {
        StringBuffer cryptText = new StringBuffer("");
        for (int i = 0; i < plainText.length() - 1; i++) {
            char currentChar = plainText.charAt(i);
            currentChar = (char) ((char) (currentChar - 'A' + 13)% 26 + 'A');
            cryptText.append(currentChar);
        if (currentChar <= 'A' && currentChar >= 'Z'){
            cryptText.append(plainText);
        }

        }
        return cryptText.toString();

    }

    /**
     * Main method of the SecretCode class
     * 
     * @param args
     */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (1 > 0) {
            System.out.println("Enter plain text to encode, or QUIT to end");
            Scanner keyboard = new Scanner(System.in);
            String plainText = keyboard.nextLine();
            if (plainText.equals("QUIT")) {
                break;
            }
            String cryptText = SecretCode.rotate13(plainText);
            String encodedText = SecretCode.rotate13(plainText);

            System.out.println("Encoded Text: " + encodedText);
        }

    }

}

在静态字符串rotate13方法和if语句中,如果字符小于'A'或大于'Z',则使密文字符与明文字符相同。我的问题是如何使密文字符与明文字符相同?我所拥有的不起作用,我完全陷入困境。任何意见是极大的赞赏。提前致谢。

最佳答案

你的条件是错误的..更改

if (currentChar <= 'A' && currentChar >= 'Z')

if (currentChar < 'A' || currentChar > 'Z')

关于java - 如何使密文字符与明文字符相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22325602/

相关文章:

java - UseConcMarkSweepGC 已弃用,它的替代品是什么?

java - 创建具有常量值的 DoubleBinding

Java 正则表达式 : Find returns true but one of the group() fails to return value

java - 如何使用正确的编码将所有控制台输出重定向到 Swing JTextArea/JTextPane?

c# - 将字符串转换为字节数组,反之亦然

.net - youtube - 视频上传失败 - 无法转换文件 - 视频编码错误?

javascript - 在 JavaScript 中实现 ROT13

java - OpenCV 安卓 : How to draw matching key points over the compared images?

xcode - 如何在 Swift 中实现 ROT13 函数?

python - ROT(n) 编码器和解码器,但解码器不工作