我试图让它返回压缩的单词。例如,反应应为@ act $。但是它以react $的形式返回。我觉得我的问题不在return语句中包含原始单词。有人可以帮忙吗?谢谢!

public static String compress (String word) {
      String newWord = "";
      int the = word.indexOf("the");
      if (the >= 0) {
         newWord = word.substring(0,the) + "&" + word.substring(the+3);
      }
      int ion = newWord.indexOf("ion");
      if (ion >= 0) {
         newWord = newWord.substring(0,ion) + "$" + word.substring(ion+3);
      }
      int ing = newWord.indexOf("ing");
      if (ing >= 0) {
         newWord = newWord.substring(0,ing) + "~" + word.substring(ing+3);
      }
      int an = newWord.indexOf("an");
      if (an >= 0) {
         newWord = newWord.substring(0,an) + "#" + word.substring(an+2);
      }
      int re = newWord.indexOf("re");
      if (re >= 0) {
         newWord = newWord.substring(0,re) + "@" + word.substring(re+2);
      }
      int con = newWord.indexOf("con");
      if (con >= 0) {
         newWord = newWord.substring(0,con) + "%" + word.substring(con+3);
      }
      return newWord;
   }

最佳答案

压缩版本还:

public static String compress(String word) {
    word = word.replace("the", "&");
    word = word.replace("ion", "$");
    word = word.replace("ing", "~");
    word = word.replace("an", "#");
    word = word.replace("re","@");
    word = word.replace("con","%");
    return word;
}

关于java - Java方法已损坏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26642438/

10-10 19:37