我已經創造了大學的一組項目的Pig Latin翻譯檢測音節(我們沒有真正讓翻譯,只需操縱任何我們想辦法一個字符串,我選擇了這個)。Scanner類:在Word
到我的翻譯輸入是一個拉丁祈禱,前兩行,其中有:
credo in unum deum
patrem omnipotentem
我已經建立了我的翻譯用下面的代碼:
public static void pigLatinify(String fname) throws IOException
{
File file = new File("projectdata.txt");
try
{
Scanner scan1 = new Scanner(file);
while (scan1.hasNextLine())
{
Scanner scan2 = new Scanner(scan1.nextLine());
boolean test2;
while (test2 = scan2.hasNext())
{
String s = scan2.next();
char firstLetter = s.charAt(0);
if (firstLetter=='a' || firstLetter=='i' || firstLetter=='o' || firstLetter=='e' ||
firstLetter=='u' || firstLetter=='A' || firstLetter=='I' || firstLetter=='O' ||
firstLetter=='E' || firstLetter=='U')
{
String output = s + "hay" + " ";
System.out.print(output);
}
else
{
String restOfWord = s.substring(1);
String output = restOfWord + firstLetter + "ay" + " ";
System.out.print(output);
}
}
System.out.println("");
}
scan1.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
它的輸出整個祈禱都很好,前兩行輸出如下:
redocay inhay unumhay eumday
atrempay omnipotentemhay
但是,在真豬拉丁文,單音節詞語保持不變,並在末尾加上「-hay」,所以「it」變成「ithay」,「egg」變成「egghay」,但是多音節詞彙在結尾加上「-way」所以「射箭」變成了「射箭」,「結局」變成了「終點」。
是否有一個Java的方式(和我使用的掃描器類)來檢測,如果一個詞是單音節?
在這一點上我也會指出我只是一個初學編程的,所以如果有,但它是非常複雜的,隨意只是說!
相關:[http://stackoverflow.com/a/405179/1225328](http://stackoverflow.com/a/405179/ 1225328) – sp00m