我正在尝试编写一个程序来检查用户支持的句子并将其转换为Pig Latin。我正在尝试让程序检查第一个字母是否是元音,并使用布尔表达式返回。接下来我试图让程序切断单词中的第一个字母并将其添加到单词的末尾。最后,如果它是一个元音,它应该添加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");
}
}
}
答案 0 :(得分:2)
尝试这样的事情 -
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
答案 1 :(得分:0)
您还可以尝试以下方式:
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;
}
句子的输出&#34;快速布朗狐狸跳过懒狗&#34;将是:
hetay uickqay rownbay oxfay umpedjay veroay hetay azylay ogday