- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个 Pig Latin 翻译器,将用户的输入翻译成 Pig Latin。我已经弄清楚了单词何时以元音开头以及何时不将第一个字母放在中间。然而,当涉及到辅音簇(单词中第一个元音之前的字符组)时,我只是不知道如何将它们集中到自己的变量中。我使用 for 循环来扫描字母中的第一个变量,然后尝试将所有这些字符串聚集到它自己的变量中,然后放入单词的中间。
这是我到目前为止的代码:
import java.util.Scanner;
public class Mission4
{
public static void main(String[] args)
{
Scanner in = new Scanner (System.in);
System.out.println("Please enter word to convert to Piglatin:");
String userInput = in.nextLine();
int firstV = 0;
char firstCh = Character.toLowerCase(userInput.charAt(0));
do
{
if (firstCh == 'a' || firstCh == 'e' || firstCh == 'i' || firstCh == 'o' || firstCh == 'u') //if userInput starts with vowel
{
String pigTalk = userInput + "ay";
System.out.println(pigTalk); // adding 'ay' to the end of their input
System.out.println("Enter another word to convert to piglatin, otherise press \"Q\" to exit.");
userInput = in.nextLine();
}
else //if userInput doesn't begin with vowel
{
for (int i = 0; i < firstV; i++)
{
char ch = userInput.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
{
firstV = Character.subString(ch);
}
}
String firstCluster = userInput.substring(0,firstV.length);
String secondCluster = userInput.substring(firstV.length,userInput.length());
System.out.println(secondCluster + firstCluster + "ay"); //Printing out their piglatin
System.out.println("Enter another word, or type \"Q\" to exit program.");
userInput = in.nextLine();
}
} while (!userInput.equals("Q")); //Giving user an exit option
}
}
你能提供什么建议吗?任何帮助表示赞赏。
最佳答案
首先,尝试将代码组织成更小的函数,形成逻辑单元。这使您和其他人更容易理解程序正在做什么以及它做错了什么。
程序中的几个错误:
firstV
设置为 0,因此该循环立即停止:for (int i = 0; i < firstV; i++)
循环需要做的是从第二个字符开始迭代,直到找到元音。
这是提取到函数中的更正后的主要逻辑,也是一个辅助函数:
public String toPigLatin(String word) {
if (isVowel(word.charAt(0))) {
return word + "ay"; // actually, wikipedia says you should append "yay", not "ay"
}
for (int i = 1; i < word.length(); i++) {
if (isVowel(word.charAt(i))) {
return word.substring(i) + word.substring(0, i) + "ay";
}
}
return word + "ay";
}
public boolean isVowel(char c) {
c = Character.toLowerCase(c);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
关于java - Pig Latin 翻译器——寻找辅音簇。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37502100/
我最近发现需要某种类型的“翻译”类型,可以将特定文本字段或区域翻译成其他语言。 我希望用户何时在 **** 中写入文本html 控件和空格后的文本应转换为本地语言即印地语、阿拉伯语 芬兰语 我不确定是
我正在做一个初学者练习,创建一个 rövarspråket 翻译器。 Write a function translate() that will translate a text into “röv
class seh_exception : public std::exception { public: seh_exception(UINT se_code, PEXCEPTION_P
如果以下代码我没有得到status 200和responseText。但此 URL 有效:http://api.microsofttranslator.com/V2/Http.svc/GetLangu
我想为 Illuminate\Translate\Translator 实现一些额外的功能. 所以,我在 ~/vendor 中创建我的文件夹目录,放在那里My/Traslator类,将实现 Symfo
我经常看到(并重用)没有适当输出的第 3 方源代码。是否有任何工具(代码翻译器)可以将 println 输出转换为合适的日志框架代码 private void processCreateTrainin
我正在制作一个 Pig Latin 翻译器,将用户的输入翻译成 Pig Latin。我已经弄清楚了单词何时以元音开头以及何时不将第一个字母放在中间。然而,当涉及到辅音簇(单词中第一个元音之前的字符组)
这就是我想要的: var elite = document.getElementById("elite"), leet = document.getElementById("leet"), a
因此,我拥有将英语翻译成 Pig Latin 的程序的全部功能。我有大约 80% 的反向翻译功能。 我有两个问题: 首先,当从 Pig Latin 翻译回英语时,它会在它遇到的第一个超过 7 个字符的
VOWELS = ('a', 'e', 'i', 'o', 'u') def pigLatin(word): first_letter = word[0] if first_lette
我有一组用 jquery 编写的表单验证规则 validator插入。由于我必须在服务器端重复相同的验证,所以我认为不必在 PHP 中重写我的规则会很好。如果规则很简单 field=>rulename
gobuffalo i18n 翻译器可在操作中访问,但如何在我的模型上访问它?它需要 buffalo.Context 作为 T *i18n.Translator 中的参数。 提前致谢! 最佳答案 这是
我一直在努力翻译我的应用程序,我从我的数据库中提取翻译并将它们添加到目录中。如果我用 $translator->trans('TEST_TRANSLATION') 回显翻译,一切都在 Symfony
这是我第一次使用 i18next,我不知道如何使它工作。 (似乎 documentation 对我来说是不完整的) 这是我使用 i18next 的 HTML 代码
是否有任何简单的方法可以将 Prolog/ASP 代码转换为 CLIPS? 类似于此,但使用 CLIPS 而不是 Prover9: https://github.com/potassco/anthem
我正在寻找一个免费的 Java 翻译 API,我发现 microsoft-translator-java-api ,它非常容易使用,我喜欢这一点,但是当我从英语翻译成阿拉伯语时,如下所示: publi
如何修改此代码以翻译页面上唯一一个 ID 为 #lyrics 的容器? function googleTranslateElementInit() { new google
我需要将 PHP 源代码编译为 LLVM 位码。我试过 Raven PHP ( http://code.roadsend.com/rphp ),它能够做我想做的事,但该项目似乎已经死了。所以我想检查一
五年多来,我一直在使用此代码在 Excel VBA 宏中将用户输入的英语文本转换为法语或德语。那是在 Microsoft Azure Marketplace 上,由于我的使用量很少,所以它是免费的。
在尝试 Azure 文本翻译 API 时,我偶然发现了应用 ID 或订阅 ID 的一些问题。文档中的应用程序 ID 似乎令人困惑,它不是可以在 Azure 门户中找到的订阅 ID,而是资源 > key
我是一名优秀的程序员,十分优秀!