将currentchar追加到crypttext的末尾,将结果保存回crypttext

wa7juj8i  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(270)
package edu.westga.secretcode;

import java.util.Scanner;

/**
 * Creates the secret code class.
 * 
 * @author Stephen Roland
 * 
 */
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) {
        String cryptText = "";
        for (int i = 0; i < plainText.length() - 1; i++) {
            char currentChar = plainText.charAt(i);
            currentChar += 13;
            cryptText.append(currentChar);

        }
        return cryptText;

    }

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

    }

}

嘿,伙计们,我有个简单的问题。如问题所述,它要求将currentchar附加到crypttext的末尾,将结果保存回crypttext。我所做的是在for循环中这样做,但它不起作用:crypttext.append(currentchar);这似乎很简单,但研究了一个小时后,我卡住了。任何帮助将非常感谢如何做到这一点。提前谢谢。

7uzetpgm

7uzetpgm1#

试试这个,

import java.util.Scanner;

/**
 * Creates the secret code class.
 * 
 * @author Stephen Roland
 * 
 */
 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(); i++) {
            char currentChar = plainText.charAt(i);
            currentChar = (char)((char)(currentChar-'A' + 13)%26 + 'A');
            cryptText.append(currentChar);

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

    }

}

相关问题