Java将字符串拆分为Pig Latin翻译器的单个单词

时间:2014-12-05 06:06:32

标签: java string

我有一个单词Pig Latin translator,我需要它

  • 翻译包含多个单词的字符串
  • 适当地利用资本
  • 正确点缀(非字母字符。即astah,= astahay,not astah,ay)

我用来测试这个的字/字符串是" Astah la vista,宝贝。"其输出应为" Astahay alay istavay,abybay。"

此代码只能正确翻译一个单词字符串,并且不能正确地大写/标点符号。 我的程序使用3个方法,input(),pigLatinator()和display()来输入字符串,翻译和显示翻译。 有了这个结构,我可以更改/添加什么来使这个程序更接近我想要的方式?我已经看过使用String.split(" \\ s")但我不了解如何实现它。

输入法:

    public String input(){
    System.out.println("Please enter your string.");
    System.out.print("String: ");
    originalString = keyboard.nextLine();
    originalString = originalString.toLowerCase(); //String.toLowerCase() to prevent error on line 41 (if() statement)

    return originalString;
}

pigLatinator方法:

    public String pigLatinator(String originalString){
    if(originalString.startsWith("a") || originalString.startsWith("e") || originalString.startsWith("i") || originalString.startsWith("o") || originalString.startsWith("u")){
        pigString = originalString + "ay";
    }else{
        pigString = originalString.substring(1) + originalString.charAt(0) + "ay";
    }

    return pigString;
}

显示方法:

public void display(){
    System.out.println();
    System.out.println("Original string: " +originalString);
    System.out.println("Translation: " +pigString);
}

输出:

输出1:

Please enter your string.
String: astah

Original string: astah
Translation: astahay

作品

输出2:

Please enter your string.
String: Astah

Original string: astah
Translation: astahay

输出3:

Please enter your string.
String: astah,

Original string: astah,
Translation: astah,ay

输出4:

Please enter your string.
String: Astah la vista, baby.

Original string: astah la vista, baby.
Translation: astah la vista, baby.ay

1 个答案:

答案 0 :(得分:1)

您可以使用String.split功能将输入句子拆分为单个单词。这是第一步,就像这样:

String[] words = originalString.split(" ");
String[] pigWords;

完成后,您可以遍历每个单词以进行更改:

pigWords = new String[words.length];
for (int i = 0; i < words.length; i++)
    pigWords[i] = pigLatinator(words[i])

然后,修改你的pigLatinator以检查单词的结尾是否有标点符号,并在该标点符号前插入“ay”:

int index = originalString.length() -1;
char lastChar = originalString.charAt(index);
if (char == "." || char == ",")
    String pigString = originalString.substring(0,index) + "ay" + originalString.charAt(index);

由于您的PigLatin课程是与您的主要课程分开的课程,因此您应该可以执行以下操作:

class PigLatin
{
    String originalString = "", pigString = "";

    public void input() {
        System.out.println("Please enter your string.");
        System.out.print("String: ");
        originalString = keyboard.nextLine().toLowerCase();
    }

    private String pigLatinator(String word)
    {
        String output = "";
        int index = word.length() -1;
        char lastChar = word.charAt(index);

        if(word.startsWith("a") || word.startsWith("e") || word.startsWith("i") || word.startsWith("o") || word.startsWith("u"))
        {
            if (char == '.' || char == ',' || char == '!')
                output = word.substring(0,index) + "ay" + word.charAt(index);
            else
                output = word + "ay";
        }
        else
        {
            if (char == '.' || char == ',' || char == '!')
                output = word.substring(1,index) + word.charAt(0) + "ay" + word.charAt(index);
            else
                output = word.substring(1) + word.charAt(0) + "ay";
        }

        return output;
    }

    public void translate()
    {
        String[] words = originalString.split(" ");
        for (int i = 0; i < words.length; i++)
        {
            pigString += pigLatinator(words[i]) + " ";
        }
    }

    public void display()
    {
        System.out.println();
        System.out.println("Original string: " +originalString);
        System.out.println("Translation: " +pigString);
    }
}

然后,在main函数内,声明一个新的PigLatin对象,并调用相应的函数:

public static void main(String[] args)
{
    PigLatin myPigLatin = new PigLatin();
    myPigLatin.input();
    myPigLatin.translate();
    myPigLatin.display();
}

此代码未经测试,但应该可以使用,这很简单。