将单词转换为Pig Latin

时间:2015-10-15 03:40:32

标签: java converter words jgrasp latin

我希望这能找到你。

我是Java的新手,也是这个网站的新手。虽然这可能看起来很长,但我只需要帮助两件事,所以请帮忙,就像我说我对这一切都是新手,所以越彻底越好。我必须做一个项目,我们必须将常规英语单词转换为pig Latin。我错过了教师想要添加的一些内容。他是我现在的代码。

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();
}
}
}

我似乎无法实现的代码是“如果单词没有元音,则打印”INVALID“”,例如,如果我在此代码中输入bcdfgh,它会读回bcdfgh ay。但它应该说无效

另一件我无法添加代码的是“如果第一个元音是”u“而前面的字母是”q“那么”u“也会到达单词的末尾。”例如,如果我在这段代码中输入问题,它会读取uestionqay。但是我想要它说estionquay。

请,谢谢

1 个答案:

答案 0 :(得分: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;
    }
}

这不能解决您的问题

“我无法添加代码的另一件事就是”如果第一个元音是“u”而前面的字母是“q”那么“u”也会到达单词的末尾。“例如,如果我在这段代码中输入问题,它会读取uestionqay。但我希望它能说出estionquay。“

你必须自己尝试这样做:)我可以回顾一下你做了什么,看看它是否有效,但我想看到你试试。