String.equals 方法在比较两个字符串时总是返回 false

我上大学的任务是编写莫尔斯电码解码器。我有字符串数组,其中包含“莫尔斯字母”的每个字母。在一个 for 循环中,我使用 substring-method 将 morsecode 中的句子切割成“morseletters”。我制作了另一个 for 循环和一个 if 语句,以检查“morse-alphabet”中的哪个字母与当前的“morseletter”匹配。我使用了 String.equals 方法,但它不起作用。即使两个字符串相同。我还打印了循环中每个字符串的长度,以检查字符串是否包含一些不需要的空格。但是即使字符串看起来相同并且长度相同,我的 if 语句的条件也永远不会正确。问题是equals-method。我必须更改什么才能使我的代码正常工作?


public class Morse {

private static String[] morsecodes = { ".-", "-...", "-.-.", "-..", ".",

         "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",

         "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--",

         "-..-", "-.--", "--.." }; 


public static void  main (String args[]) {

    System.out.println(decodeMorseCode(".... . .-.. .-.. --- .-- --- .-. .-.. -.."));       

}

public static String decodeMorseCode(String morseText) {

    String realText = "";

    String morseLetter = "";

    int counter = 0;


    for(int i = 0; i < morseText.length(); i++) {   

        if((morseText.charAt(i)==' ')) {

            morseLetter = morseText.substring(counter,i);

            counter = i+1;

        }

        if(morseText.charAt(i)==' '||i+1==morseText.length()) {

            for (int j = 0; j < 26; j++) {

                if((morsecodes[j].equals(morseLetter))) { //this is the if-statemen which causes the problem

                    char c = (char)(j+97);

                    realText += c;

                }


                if(j+1<=morseText.length()) {

                    if(morseText.charAt(j)==' '&& morseText.charAt(j+1)==' ') {

                        realText += " ";

                    }

                }


            morseLetter = "";

            }

        }           

    }

    return realText;

}

}


哆啦的时光机
浏览 208回答 2
2回答

UYOU

morseLetter = "";如下所示 删除该行,您的代码将起作用。&nbsp; &nbsp; if(morseText.charAt(i)==' '||i+1==morseText.length()) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < 26; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(morsecodes[j].equals(morseLetter)) { //this is the if-statemen which causes the problem&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char c = (char)(j+97);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; realText += c;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(j+1<=morseText.length()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(morseText.charAt(j)==' '&& morseText.charAt(j+1)==' ') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; realText += " ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //morseLetter = "";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp;&nbsp;

慕后森

已经有一些答案,但也许你会想考虑这种方法:public static String decodeMorseCode(String morseText) {&nbsp; &nbsp; String realText = "";&nbsp; &nbsp; String morseLetter = "";&nbsp; &nbsp; int counter = 0;&nbsp; &nbsp; List<String> morseMessage;&nbsp; &nbsp; //Easier splitting of the String with morse code&nbsp; &nbsp; String[] morseLetters = morseText.split(" ");&nbsp; &nbsp; morseMessage = Arrays.asList(morseLetters);&nbsp; &nbsp; for (String morse : morseMessage) {&nbsp; &nbsp; &nbsp; &nbsp; for (String letter : morsecodes) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (morse.equals(letter)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(letter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Here comes mapping from Morse-to-English.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return realText;}我在这里所做的是引入一个列表,它将逐个字母地存储您的消息,这使得循环更容易。这是通过首先将字符串拆分为包含单个字母的数组,然后将其转换为列表来完成的。然后,我将摩尔斯电码消息中的每个字母与您的摩尔斯电码“字典”进行比较,如果您运行它,您将看到它能够识别每个单独的字母。剩下的就是创建一个 Map 来将摩尔斯电码表示翻译成英文字母。 注意:也许最好不要有一组摩尔斯电码,而是将其作为带有英文映射的 Map?例如:private static Map<Character, String> morseToEnglish;public static void&nbsp; main (String[] args) {&nbsp; &nbsp; morseToEnglish = new HashMap<Character, String>();&nbsp; &nbsp; morseToEnglish.put('a', ".-");&nbsp; &nbsp; morseToEnglish.put('b', "-...");&nbsp; &nbsp; morseToEnglish.put('c',&nbsp; "-.-");&nbsp; &nbsp; morseToEnglish.put('d',&nbsp; "-..");&nbsp; &nbsp; ...然后循环和映射:&nbsp; &nbsp; for (String morse : morseMessage) {&nbsp; &nbsp; &nbsp; &nbsp; for (String letter : morseToEnglish.values()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (morse.equals(letter)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Character character : morseToEnglish.keySet()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (morseToEnglish.get(character).equals(letter)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(character);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }你用莫尔斯编码的信息是'helloworld'.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java