我的任务是使用递归创建猪拉丁语翻译器,可以翻译句子。规则如下:抛光我的猪拉丁语翻译器(代码)(Java)
如果英语中没有元音字母,那么pigLatinWord只是英文字+“ay”。 (有10个元音:'a','e','i','o'和'u',以及它们的大写字母。'y'不被认为是用于这个任务的元音,即我变成了myay,为什么变成了whyay等)
否则,如果englishWord以元音开头,那么pigLatinWord只是englishWord +“yay”。
否则(如果englishWord中有一个元音,但不与元音开始),然后pigLatinWord是端+开始+“AY”,其中结束和开始的定义如下: -
让我们开始使用所有的englishWord(但不包括)其第一个元音。 让我们结束从其第一个元音的所有英语词汇。 但是,如果英语字词大写,那么大写结尾和“非大写字母”开始。
这里是我到目前为止的代码(抱歉怪异的格式,我的意见弄乱):
/*Recursively Translate a String (without punctuation or numerical characters) to Pig Latin*/
//prep the string for translation and submit it to be translated
public static String translate(String finished) {
finished.trim(); //Trim the String of whitespace at the front and end
finished += " "; //Because of the recursive method I use, the string must have a
//space at the end
finished = translateSentence(finished); //recursively translate the string
finished.trim(); //trim the whitespace added earlier
return finished; //Return the string
}
//recursively submits each word in the string to the translator, then
//returns the translated sentence
private static String translateSentence(String finished) {
if (finished.length() == 0) { //the base condition is met when each word in the string
return finished; //has been sent to the translator (string is empty)
}
else {
return (translateWord(finished.substring(0, finished.indexOf(' '))) + " "
+ translateSentence(finished.substring(finished.indexOf(' ') + 1)));
}
}
/*If the base condition is not met, the method returns the first word of the string
* (translated) and a space, then submits the rest of the
* string back to the method. The first word is defined as the beginning
* of the string up until the first space. The rest of the string
* starts one character after the space. */
//Checks the submitted word for vowels and vowel placement, and translates accordingly
private static String translateWord(String stringA) {
if (stringA.indexOf('a') == -1
&& stringA.indexOf('e') == -1 //Checks for presence of any vowels
&& stringA.indexOf('i') == -1 //if no vowels are found
&& stringA.indexOf('o') == -1 //the word + ay is returned
&& stringA.indexOf('u') == -1
&& stringA.indexOf('A') == -1
&& stringA.indexOf('E') == -1
&& stringA.indexOf('I') == -1
&& stringA.indexOf('O') == -1
&& stringA.indexOf('U') == -1) {
return stringA + "ay";
}
if (stringA.charAt(0) == 'a'
|| stringA.charAt(0) == 'e' //checks if there is a vowel at the start
|| stringA.charAt(0) == 'i'//of the string. if there is a vowel
|| stringA.charAt(0) == 'o' //it returns the word + yay
|| stringA.charAt(0) == 'u'
|| stringA.charAt(0) == 'A'
|| stringA.charAt(0) == 'E'
|| stringA.charAt(0) == 'I'
|| stringA.charAt(0) == 'O'
|| stringA.charAt(0) == 'U') {
return stringA + "yay";
}
/* if the word has a vowel that isn't at the start, the part of the string
* before the first vowel is moved to the end of the vowel, and "ay" is added.
* However, if the first character in the word is capitalized, the first vowel becomes
* uppercase and the former first character in the word becomes lowercase */
else {
if (Character.isUpperCase(stringA.charAt(0))) {
return Character.toUpperCase(stringA.charAt(firstVowel(stringA, 0)))
+ stringA.substring(firstVowel(stringA, 0) + 1, stringA.length())
+ Character.toLowerCase(stringA.charAt(0))
+ stringA.substring(1, firstVowel(stringA, 0)) + "ay";
}
else {
return stringA.substring(firstVowel(stringA, 0), stringA.length())
+ stringA.substring(0, firstVowel(stringA, 0)) + "ay";
}
}
}
//Recursively determines the index number of the first vowel in a given word
//0 must always be submitted as int x
public static int firstVowel(String stringA, int x) {
if (x > stringA.length() - 1) { //if the index number becomes greater than the length
return -1; //of the string, -1 (no vowels) is returned
}
if (stringA.charAt(x) == 'a'
|| stringA.charAt(x) == 'e' //the base condition is met when the character
|| stringA.charAt(x) == 'i' //at the current index number is a vowel
|| stringA.charAt(x) == 'o' //and the index number is returned
|| stringA.charAt(x) == 'u'
|| stringA.charAt(x) == 'A'
|| stringA.charAt(x) == 'E'
|| stringA.charAt(x) == 'I'
|| stringA.charAt(x) == 'O'
|| stringA.charAt(x) == 'U') {
return x;
}
else {
return firstVowel(stringA, x + 1); //otherwise, the string and the index number
} // + 1 are submitted back to the method
}
这给了我所需要的输出(“为什么你好”变成了“Whyay ellohay erethay”) 但现在它不能处理标点符号。基本上我正在寻找的是任何提示或帮助让我的代码处理标点符号,或任何方式来改善我的代码(仍然使用递归)。
2012-11-22
Philip
+1
一个改进是将所有元音放在一个字符串常量中,并使用'contains()'检查一个字符是否为元音。此外,'firstVowel(stringA,0)'可以被提取到一个局部变量中,这将进一步提高可读性。 –
+0
你能澄清有关contains()方法的部分吗?我不太了解如何将字符串常量,contains()和我正在检查的字符串关联起来。谢谢。 –
+1
我的意思是这样的:'private static final String VOWELS =“aeiouAEIOU”;',然后当你检查元音时,你做'private static boolean isVowel(char ch){return VOWELS.contains(String.valueOf (ch)); }'。阅读起来比“if”陈述容易得多。 –