英文:
need to compensate for whitespace in a morsecode decoder
问题
这是您的代码的翻译部分:
public static String Decode(String morseCode) {
String MLetter = ""; // Morse数组以容纳单个字符
String ELetter = ""; // 英文字母字符串
morseCode += " "; // 添加两个空格以处理末尾的空白
for (int i = 0; i < morseCode.length(); i++) // 将字符放入Morse字符串
{
if (morseCode.substring(i, i + 1).equals(" ")) // 如果检测到空格
{
for (int j = 0; j < morse.length; j++) // 然后将字符串与morse数组进行比较
{
if (MLetter.equals(morse[j])) // 如果Morse字符串等于morse索引
{
ELetter += english[j]; // 添加英文等效
MLetter = ""; // 并清除Morse字符串
i++; // 跳过空格
}
}
}
MLetter += morseCode.substring(i, i + 1); // 将字符添加到Morse字符串
}
return ELetter.toUpperCase();
}
请注意,为了处理输入开头和结尾的空格,我在输入的末尾添加了两个空格,并将其与已有的空格分隔开来。这应该解决了您提到的问题。
英文:
So I made a Morse Code decoder that works pretty well. the only problem is that it doesn't compensate for whitespace at the beginning and end of the entry. So say that I have "." which is E in morse code. I should be able to input "__._" and get the same result, yet I simply get an exception. Ive tried using the trim method on the entry when it comes in, and that gets rid of the exception but I get no output. Ive tried using a loop to get rid of the front whitespace but I get no output, and even if that worked then I would still need to figure out how to handle the whitespace in the back. Here's my code:
static String[] english = { " ", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
"y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
",", ".", "?","!","@","&","(",")"}; //Defining a Character Array of the English Letters numbers and Symbols so that we can compare and convert later
static String[] morse = { " " ,".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
"..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
"-----", "--..--", ".-.-.-", "..--..","-.-.--",".--.-.",".-...","-.--.","-.--.-"};
public static String Decode (String morseCode)
{
String MLetter= "";//Morsec array to hold individual characters
String ELetter ="";//English Letter String
morseCode+=" ";
for(int i=0; i<morseCode.length();i++)//put characters into the morse String
{
if(morseCode.substring(i,i+1).equals(" "))//if a space is detected
{
for(int j=0; j<morse.length;j++)// then compare the string to the morse array
{
if(MLetter.equals(morse[j]))//if the morse string equals the morse index
{
ELetter+=english[j];//add the english equivilent
MLetter="";//and clear the Morse string
i++;//Skip the space
}
}
}
MLetter+= morseCode.substring(i,i+1);//add the character to the Morse String
}
return ELetter.toUpperCase();
}
答案1
得分: 1
丑陋但有效的代码部分:
String MLetter = ""; // 用于保存单个字符的摩尔斯数组
String ELetter = ""; // 英文字母字符串
String morseCodeTrimmed = "";
// 去除前端的空格
while (morseCode.startsWith(" ")) {
morseCode = morseCode.substring(1, morseCode.length());
}
// 去除末尾的空格
while (morseCode.endsWith(" ")) {
morseCode = morseCode.substring(0, morseCode.length() - 1);
}
// 由于某种原因,我不太确定,需要将两个空格重新添加到字符串的末尾。
// 一个好的解决方案不应该有这个。
morseCodeTrimmed = morseCode + " ";
请注意,我只翻译了代码部分,没有包括注释。
英文:
Ugly but works:
String MLetter= "";//Morsec array to hold individual characters
String ELetter ="";//English Letter String
String morseCodeTrimmed = "";
// trim front white spaces
while (morseCode.startsWith(" ")) {
morseCode = morseCode.substring(1, morseCode.length());
}
// trim ending white spaces
while (morseCode.endsWith(" ")) {
morseCode = morseCode.substring(0, morseCode.length() - 1);
}
// for some reason that I'm not quite sure, 2 white spaces need
// to be added back to the end of the string.
// A good solution will not have this.
morseCodeTrimmed = morseCode + " ";
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论