我试图写一个程序,检查用户supploed句子并将其转换为隐语。 我想有程序检查,看是否第一个字母是元音或不并返回使用布尔表达式。 接下来,我试图让程序以切断从单词的首字母,并把它添加到单词的末尾。 最后,它应该补充way
,如果它是一个元音和ay
,如果它不是元音。 任何帮助或建议将不胜感激。
public class PigLatin {
public static void main(String[] argv) {
if (argv.length > 0) {
for (int i = 0; i < argv.length; i++) {
char firstLetter = aStringVariable.charAt(0);
}
}
public static boolean isVowel(char c) {
char[] vowels = new char[] {'a', 'e', 'i', 'o', 'u', 'y'};
for(int i = 0; i < vowels.length; i++) {
if(Character.toString(vowels[i]).equalsIgnoreCase(Character.toString(c))) {
return true;
}
}
return false;
}
public static String makePigLatin(boolean vowel, String word)
{
String everythingButTheFirstLetter = word.substring(1);
String n = everythingButTheFirstLetter + firstLetter;
if (true)
{
System.out.println(n + "way");
}
if (false)
{
System.out.println(n + "ay");
}
}
}
尝试这样的事情 -
public static class PigLatin {
public static boolean isVowel(char c) {
switch (Character.toLowerCase(c)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
return true;
}
return false;
}
public static String makePigLatin(String word) {
char firstLetter = word.charAt(0);
String everythingElse = word.substring(1);
String n = everythingElse + firstLetter;
if (isVowel(firstLetter)) {
return n + "way";
}
return n + "ay";
}
}
public static void main(String args[]) {
String[] words = { "fair", "away", "test" };
for (String word : words) {
String s = PigLatin.makePigLatin(word);
System.out.println(word + " is " + s);
}
}
输出
fair is airfay
away is wayaway
test is esttay
您也可以尝试这样的:
public static string PigLatin(string sentence)
{
const string vowels = "AEIOUaeiou";
sentence = sentence.ToLower();
var returnSentence = "";
foreach (var word in sentence.Split())
{
var firstLetter = word.Substring(0, 1);
var restOfWord = word.Substring(1, word.Length - 1);
var currentLetter = vowels.IndexOf(firstLetter, StringComparison.Ordinal);
if (currentLetter == -1)
{
returnSentence += restOfWord + firstLetter + "ay ";
}
else
{
returnSentence += word + "way ";
}
}
returnSentence = returnSentence.TrimEnd();
return returnSentence;
}
对于这句话的输出“敏捷的棕色狐狸跳过了懒狗”将是:
hetay uickqay rownbay oxfay umpedjay veroay hetay azylay ogday
文章来源: How to translate English into PigLatin with java (using boolean and string)