2015-10-15 98 views
1

我希望這能找到你。將單詞轉換成豬拉丁語

我對Java和本網站來說還是比較新的東西。雖然這可能看起來很長,但我只需要兩件事情的幫助,所以請幫助,而且就像我說的,我對所有這些都是新手,所以越徹底越好。我必須做一個項目,我們必須將常規英語單詞轉換成豬拉丁語。我錯過了我們的教練想要添加的一些東西。他是我現在的代碼。

import java.util.Scanner; 
public class PigLatin 
{ 
public static void main(String[] args) 
{ 
Scanner sc = new Scanner(System.in); 
final String vowels = "aeiouAEIOU"; 
System.out.println("Enter your word."); 
String word = sc.nextLine(); 
while (!word.equalsIgnoreCase("done")) 
{ 
String beforVowel = ""; 
int cut = 0; 
while (cut < word.length() && !vowels.contains("" + word.charAt(cut))) 
{ 
beforVowel += word.charAt(cut); 
cut++; 
} 
if (cut == 0) 
{ 
cut = 1; 
word += word.charAt(0) + "w"; 
} 
System.out.println(word.substring(cut) + beforVowel + "ay"); 
System.out.println("Enter your word."); 
word = sc.nextLine(); 
} 
} 
} 

我不能的代碼似乎是實施「如果單詞沒有元音,打印‘無效’」,例如,如果我在bcdfgh鍵入這段代碼,它讀回bcdfgh唉。但它應該說無效

另一件事我不能添加代碼是這個「如果第一個元音是一個」你「,並且它之前的信是一個」Q「那麼」U「也會到這個單詞。」例如,如果我在此代碼中輸入問題,它會讀取uestionqay。但我想說它estionquay。

請和謝謝

+0

我看到你拒絕了我的編輯。這是爲什麼?我只是想幫忙。 –

+0

即時對不起,我認爲這些編輯是電腦生成的,其中一個編輯完全改變了我想說的話。如果我知道用戶編輯了它,我將永遠不會拒絕它。對不起 – user10101010

回答

2

嘿,所以我的東西編碼爲您...

import java.util.Scanner; 

public class PigLatin 
{ 
    public static void main(String[] args) 
    { 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Enter your word."); 
     String word = ""; 
     word = sc.nextLine(); 
     do 
     { 
      if(word.length() > 0) 
      { 
       if(containsVowels(word.substring(0,1))) 
       { 
        System.out.println(word+"way"); 
       } 
       else 
       { 
        if(containsVowels(word)) 
        { 
         System.out.println(word.substring(1,word.length())+word.substring(0,1)+"ay"); 
        } 
        else 
         System.out.println("INVALID"); 
       } 
      } 
      System.out.println("Enter your word."); 
     } 
     while(!((word = sc.nextLine()).equalsIgnoreCase("done"))); 
     sc.close(); 
    } 

    public static boolean containsVowels(String word) 
    { 
     String[] vowels = { 
       "a","e","i","o","u" 
     }; 
     for(int i = 0; i < vowels.length; i++) 
     { 
      if(word.contains(vowels[i]) || word.contains(vowels[i].toUpperCase())) 
       return true; 
     } 
     return false; 
    } 
} 

這不能解決您的問題FOR

「另一件事情,我不能爲添加代碼是這樣的:「如果第一個元音是」u「,而它之前的字母是」q「,那麼這個」u「也會到這個單詞的末尾。」例如,如果我在這個代碼中輸入問題,它會讀取uestionqay。但我想讓它說estionquay。「

你必須嘗試這樣做你自己:)我可以檢查你做了什麼,看看它是否會工作,但我想看到你嘗試。

+0

非常感謝。我真的不能夠感謝你。生病回來喲你多一些工作後,再次感謝你 – user10101010

+0

@ user10101010沒問題,如果你想請做標記爲答案。 – 3kings