编码/解码 解码和编码字符串两次的脚本



基本上有一个简单的过程,涉及向前映射字母和数字 2 空格,因此A将编码为 C13 等。有环绕,所以Z编码为B0编码为2。编码工作正常,但在循环的第一个循环之后,解码似乎映射回 4 个空间而不是 2个。这是代码,让我知道你们认为问题是什么。

编码/解码

public class EncodeDecode {
    private String[] originalList;
    private String[] encodedList;
    private String[] decodedList;
    public EncodeDecode(String[] oL) {
        originalList = oL;
        encodedList = originalList;
        decodedList = originalList;
        for (int cnt = 0; cnt < oL.length; cnt++) {
            encodedList[cnt] = originalList[cnt];
        }
        for (int cnt = 0; cnt < oL.length; cnt++) {
            decodedList[cnt] = originalList[cnt];
        }
    }
    public String encode(String originalWord) {
        //Maps every character in original word to 2 positions forward w/ wrap around.
        String result = "";
        for (int cnt = 0; cnt < originalWord.length(); cnt++) {
            result += forwardMap(originalWord.charAt(cnt));
        }
        encodedList[0] = result;
        return result;
    }
    public String decode(String codedWord) {
        //Maps every character back 2 spaces w/ wrap around
        String result = "";
        for (int cnt = 0; cnt < codedWord.length(); cnt++) {
            result += backMap(codedWord.charAt(cnt));
        }
        decodedList[0] = result;
        return result;
    }
    public char forwardMap(char ch) {
        char result = Character.toLowerCase(ch);
        if (result == 'a') return 'c';
        if (result == 'b') return 'd';
        if (result == 'c') return 'e';
        if (result == 'd') return 'f';
        if (result == 'e') return 'g';
        if (result == 'f') return 'h';
        if (result == 'g') return 'i';
        if (result == 'h') return 'j';
        if (result == 'i') return 'k';
        if (result == 'j') return 'l';
        if (result == 'k') return 'm';  
        if (result == 'l') return 'n';
        if (result == 'm') return 'o';
        if (result == 'n') return 'p';
        if (result == 'o') return 'q';
        if (result == 'p') return 'r';
        if (result == 'q') return 's';
        if (result == 'r') return 't';
        if (result == 's') return 'u';
        if (result == 't') return 'v';
        if (result == 'u') return 'w';
        if (result == 'v') return 'x';
        if (result == 'w') return 'y';
        if (result == 'x') return 'z';
        if (result == 'y') return 'a';
        if (result == 'z') return 'b';  
        if (result == '0') return '2';
        if (result == '1') return '3';
        if (result == '2') return '4';
        if (result == '3') return '5';
        if (result == '4') return '6';
        if (result == '5') return '7';
        if (result == '6') return '8';
        if (result == '7') return '9';
        if (result == '8') return '0';
        if (result == '9') return '1';
        if (result == ' ')
            return ' ';
        else 
            return '$';     
    }
    public char backMap(char ch) {
        char result = Character.toLowerCase(ch);
        if (result == 'c') return 'a';
        if (result == 'd') return 'b';
        if (result == 'e') return 'c';
        if (result == 'f') return 'd';
        if (result == 'g') return 'e';
        if (result == 'h') return 'f';
        if (result == 'i') return 'g';
        if (result == 'j') return 'h';
        if (result == 'k') return 'i';
        if (result == 'l') return 'j';
        if (result == 'm') return 'k';  
        if (result == 'n') return 'l';
        if (result == 'o') return 'm';
        if (result == 'p') return 'n';
        if (result == 'q') return 'o';
        if (result == 'r') return 'p';
        if (result == 's') return 'q';
        if (result == 't') return 'r';
        if (result == 'u') return 's';
        if (result == 'v') return 't';
        if (result == 'w') return 'u';
        if (result == 'x') return 'v';
        if (result == 'y') return 'w';
        if (result == 'z') return 'x';
        if (result == 'a') return 'y';
        if (result == 'b') return 'z';  
        if (result == '2') return '0';
        if (result == '3') return '1';
        if (result == '4') return '2';
        if (result == '5') return '3';
        if (result == '6') return '4';
        if (result == '7') return '5';
        if (result == '8') return '6';
        if (result == '9') return '7';
        if (result == '0') return '8';
        if (result == '1') return '9';
        if (result == ' ')
            return ' ';
        else 
            return '$';     
    }
    public String[] getEncodedList() {
        return encodedList;
    }
    public String[] getDecodedList() {
        return decodedList;
    }
}

测试 仪

public class EncodeDecodeTester {
    public static void main(String[] args) {
        String[] list = {"abcd", "abcd", "abcd"};
        EncodeDecode ec = new EncodeDecode(list);
        String[] encode = ec.getEncodedList();
        for (int index = 0; index < encode.length; index++) {
            System.out.println("Encoded: " +ec.encode(list[index]));
            System.out.println("Decoded: " +ec.decode(list[index]));
        }
    }
}

我注意到的第一件事是String[]上的对象引用,更改此

encodedList = originalList;
decodedList = originalList;

encodedList = new String[originalList.length];
decodedList = new String[originalList.length];

NOtice在主函数中,您正在对实际字符串而不是编码字符串调用解码。 因此,如果初始化正确,如 Elliot 指出的那样,您的程序将正常工作。

for (int index = 0; index < encode.length; index++)
{
    System.out.println("Encoded: " +ec.encode(list[index]));
    System.out.println("Decoded: " +ec.decode(ec.encode(list[index])));
}

这将为您提供所需的输出

经过足够的修补和室友的想法后,最终找到了答案。

更改了此内容:

public String decode(String codedWord)
{
//Maps every character back 2 spaces w/ wrap around
String result = "";
for (int cnt = 0; cnt < codedWord.length(); cnt++)
    {
        result += backMap(codedWord.charAt(cnt));
    }
    decodedList[0] = result;
return result;
}

对此:

public String decode(String codedWord)
{
    //Maps every character back 2 spaces w/ wrap around
    String result = "";
    for (int cnt = 0; cnt < codedWord.length(); cnt++)
        {
            result += backMap(encodedList[0].charAt(cnt));
        }
        decodedList[0] = result;
    return result;
}

相关内容

  • 没有找到相关文章

最新更新