我有一个单词Pig Latin translator,我需要它
我用来测试这个的字/字符串是" 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;
}
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);
}
Please enter your string.
String: astah
Original string: astah
Translation: astahay
作品
Please enter your string.
String: Astah
Original string: astah
Translation: astahay
Please enter your string.
String: astah,
Original string: astah,
Translation: astah,ay
Please enter your string.
String: Astah la vista, baby.
Original string: astah la vista, baby.
Translation: astah la vista, baby.ay
答案 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();
}
此代码未经测试,但应该可以使用,这很简单。