Pregunta

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

    }

}

In static String rotate13 method and in the if statement, if the character is less than 'A' or greater than 'Z', make the cryptText character the same as the plainText character. My question is how do I make the cryptText character the same as the plaintext character? What I have is not working and I am totally stuck on this. Any advice is greatly appreciated. Thanks in advance.

¿Fue útil?

Solución

Your condition is wrong..Change

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

to

if (currentChar < 'A' || currentChar > 'Z')
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top