2017-07-03 87 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()功能