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

rlcwz9us  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(406)
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”,则使crypttext字符与纯文本字符相同。我的问题是如何使密文字符与明文字符相同?我所拥有的是没有工作,我完全被困在这个。任何建议都非常感谢。提前谢谢。

yquaqz18

yquaqz181#

你的情况不对…改变

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

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

相关问题