将单词转换为 Pig Latin
Converting words to Pig Latin
希望你一切顺利。
我对 Java 和这个网站还很陌生。虽然这看起来很长,但我只需要两件事的帮助,所以请帮忙,就像我说的,我对这一切都是新手,所以越彻底越好。我必须做一个项目,我们必须将常规英语单词转换为 pig Latin。我遗漏了一些我们的讲师想要添加的内容。这是我现在拥有的代码。
import java.util.Scanner;
public class PigLatin
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final String vowels = "aeiouAEIOU";
System.out.println("Enter your word.");
String word = sc.nextLine();
while (!word.equalsIgnoreCase("done"))
{
String beforVowel = "";
int cut = 0;
while (cut < word.length() && !vowels.contains("" + word.charAt(cut)))
{
beforVowel += word.charAt(cut);
cut++;
}
if (cut == 0)
{
cut = 1;
word += word.charAt(0) + "w";
}
System.out.println(word.substring(cut) + beforVowel + "ay");
System.out.println("Enter your word.");
word = sc.nextLine();
}
}
}
我似乎无法实现的代码是 "If the word has no vowel, print "INVALID"",例如,如果我在此代码中输入 bcdfgh,它会读回 bcdfgh ay。但它应该说无效
我不能添加代码的另一件事是这个 "If the first vowel is a "u" 和它前面的字母是 "q" 然后 "u" 也到单词的末尾。 “例如,如果我在此代码中输入问题,它会显示 uestionqay。但我想让它说 estionquay。
拜托,谢谢
嘿,所以我为你编写了一些代码...
import java.util.Scanner;
public class PigLatin
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your word.");
String word = "";
word = sc.nextLine();
do
{
if(word.length() > 0)
{
if(containsVowels(word.substring(0,1)))
{
System.out.println(word+"way");
}
else
{
if(containsVowels(word))
{
System.out.println(word.substring(1,word.length())+word.substring(0,1)+"ay");
}
else
System.out.println("INVALID");
}
}
System.out.println("Enter your word.");
}
while(!((word = sc.nextLine()).equalsIgnoreCase("done")));
sc.close();
}
public static boolean containsVowels(String word)
{
String[] vowels = {
"a","e","i","o","u"
};
for(int i = 0; i < vowels.length; i++)
{
if(word.contains(vowels[i]) || word.contains(vowels[i].toUpperCase()))
return true;
}
return false;
}
}
这不能解决您
的问题
"one other thing i cant add code for is this "如果第一个元音是 "u" 并且它之前的字母是 "q" 那么 "u" 也会到单词的末尾。”例如,如果我在此代码中输入问题,它会显示 uestionqay。但我希望它说 estionquay。"
你必须自己尝试这样做 :) 我可以回顾你所做的,看看它是否有效,但我希望看到你尝试这样做。
希望你一切顺利。
我对 Java 和这个网站还很陌生。虽然这看起来很长,但我只需要两件事的帮助,所以请帮忙,就像我说的,我对这一切都是新手,所以越彻底越好。我必须做一个项目,我们必须将常规英语单词转换为 pig Latin。我遗漏了一些我们的讲师想要添加的内容。这是我现在拥有的代码。
import java.util.Scanner;
public class PigLatin
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final String vowels = "aeiouAEIOU";
System.out.println("Enter your word.");
String word = sc.nextLine();
while (!word.equalsIgnoreCase("done"))
{
String beforVowel = "";
int cut = 0;
while (cut < word.length() && !vowels.contains("" + word.charAt(cut)))
{
beforVowel += word.charAt(cut);
cut++;
}
if (cut == 0)
{
cut = 1;
word += word.charAt(0) + "w";
}
System.out.println(word.substring(cut) + beforVowel + "ay");
System.out.println("Enter your word.");
word = sc.nextLine();
}
}
}
我似乎无法实现的代码是 "If the word has no vowel, print "INVALID"",例如,如果我在此代码中输入 bcdfgh,它会读回 bcdfgh ay。但它应该说无效
我不能添加代码的另一件事是这个 "If the first vowel is a "u" 和它前面的字母是 "q" 然后 "u" 也到单词的末尾。 “例如,如果我在此代码中输入问题,它会显示 uestionqay。但我想让它说 estionquay。
拜托,谢谢
嘿,所以我为你编写了一些代码...
import java.util.Scanner;
public class PigLatin
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your word.");
String word = "";
word = sc.nextLine();
do
{
if(word.length() > 0)
{
if(containsVowels(word.substring(0,1)))
{
System.out.println(word+"way");
}
else
{
if(containsVowels(word))
{
System.out.println(word.substring(1,word.length())+word.substring(0,1)+"ay");
}
else
System.out.println("INVALID");
}
}
System.out.println("Enter your word.");
}
while(!((word = sc.nextLine()).equalsIgnoreCase("done")));
sc.close();
}
public static boolean containsVowels(String word)
{
String[] vowels = {
"a","e","i","o","u"
};
for(int i = 0; i < vowels.length; i++)
{
if(word.contains(vowels[i]) || word.contains(vowels[i].toUpperCase()))
return true;
}
return false;
}
}
这不能解决您
的问题"one other thing i cant add code for is this "如果第一个元音是 "u" 并且它之前的字母是 "q" 那么 "u" 也会到单词的末尾。”例如,如果我在此代码中输入问题,它会显示 uestionqay。但我希望它说 estionquay。"
你必须自己尝试这样做 :) 我可以回顾你所做的,看看它是否有效,但我希望看到你尝试这样做。