2015-10-15 87 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