2017-07-03 96 views
-1

我正在尝试将您的句子翻译成拉丁语的程序。下面的代码我到目前为止:猪拉丁语句子转换器

public class PigLatin { 
    public static void main(String[] args) { 
     //Enter text in the quotes of System.ot.println(covertToLatin(" ")); 
     System.out.println(covertToLatin("Type your sentence here.")); 
    } 

    private static String covertToLatin(String string) { 
     String end = ""; 
     String delims = "\\s+"; 
     String[] words = string.split(delims); 
     for (int i = 0; i < words.length; i++) { 
      if(isVowel(words[i].toLowerCase().charAt(0))) { 
       end += words[i] + "ay"; 
      } else { 
       end += words[i].substring(1) + words[i].substring(0, 1) + "ay"; 
      } 
     } 
     return end; 
    } 

    private static boolean isVowel(char c) { 
     if (c == 'a') 
      return true; 
     if (c == 'e') 
      return true; 
     if (c == 'i') 
      return true; 
     if (c == 'o') 
      return true; 
     if (c == 'u') 
      return true; 
     return false; 
    } 

} 

它翻译“在这里输入你的句子。” to“ypeTayouryayentencesayere.hay”我很难找到一种方法来翻译我的整个句子。你能帮我把整句翻译成拉丁文吗?另外,如果你能找到一种方法让句子在所有大写字母上转换,它也会有所帮助。

+0

对不起,我很困惑你的问题是什么,什么不是你想要的? –

+0

它不工作,只是没有空格?如果我清理你的输出,这是合理的拉丁语:'ypeTay ouryay entencesay ere.hay'。我认为你只需要在单词之间添加空格。 – Ian

+0

它看起来像你的结果几乎是正确的,除了一些缺陷。你应该能够解决这些问题。 – ajb

回答

0

首先翻译一个词,然后是一个完整的句子。例如STACK应打印出ACKSTAY。你的程序打印出TACKSAY。 这是为什么?让我们来看看你的逻辑:

  for (int i = 0; i < words.length; i++) { 
         if(isVowel(words[i].toLowerCase().charAt(0))) { 
          end += words[i] + "ay"; 
         } else { 
    /*substring(1) is always 1 && 
    you add substring(0,1) which is always the interval (0,1) they never change*/ 

      end += words[i].substring(1) + words[i].substring(0, 1) +ay"; 
      } 
     } 
    return end.toUpperCase(); 
       } 

      private static boolean isVowel(char c) { 
       if ((c == 'a') | (c == 'e') | (c == 'i') | (c == 'o') | (c == 'u')) 
        return true; 
       return false; 
       } 

尝试先在纸上写你的算法。例如总是使用单词堆栈。

第一个字母是一个s(不是元音),我们把它保存在一个临时字符串中。 第二个字母是t(不是元音),我们把它保存在一个临时字符串中。 a是一个元音!我们从在温度一起+字母+ AY

最终结果打印= “ACK” + “ST” + “AY”

抽象 - >子(I,endOfString)+子串(K,I) +“AY

所以实际需要两个计数器!I,K用于打印SUBSTRING(I,EndOfString)和子串(I,K)表示临时阵列

1

为大写,使用String.toUpperCase()功能