基本上有一个简单的过程,涉及将字母和数字向前映射2个空格,以便A
编码为C
,1
到3
等等。因此,Z
将编码为B
并且0
将编码为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]));
}
}
}
答案 0 :(得分:0)
我注意到的第一件事是String[]
上的对象引用,更改此
encodedList = originalList;
decodedList = originalList;
到
encodedList = new String[originalList.length];
decodedList = new String[originalList.length];
答案 1 :(得分:0)
请注意,在主函数中,您在实际字符串上调用解码而不在编码字符串上调用。所以如果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])));
}
这将为您提供所需的输出
答案 2 :(得分:0)
在经过足够的修补和对我的室友反弹的想法之后结束找到答案。
改变了这个:
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;
}