Java 如何将用户给定的字符串转换为Pig拉丁语?
我正在尝试将从用户处获取的字符串转换为Pig拉丁语。我不能使用任何特殊的类、方法或数组。除了任何类型的循环外,我只能使用扫描仪创建一个对象,从用户和.length和.charAt获取字符串。(也不能使用switch语句或break关键字) 下面是我的输出的示例:Java 如何将用户给定的字符串转换为Pig拉丁语?,java,string,loops,Java,String,Loops,我正在尝试将从用户处获取的字符串转换为Pig拉丁语。我不能使用任何特殊的类、方法或数组。除了任何类型的循环外,我只能使用扫描仪创建一个对象,从用户和.length和.charAt获取字符串。(也不能使用switch语句或break关键字) 下面是我的输出的示例: Enter a line of text: this is a test. Input : this is a line of text. Output: his-tay is-way a-way ine-lay of-way ext
Enter a line of text: this is a test.
Input : this is a line of text.
Output: his-tay is-way a-way ine-lay of-way ext-tay.
这是我的代码,我只能让我的代码与一个字,它必须有一个空间的结尾。根据循环的不同,一次只能有一个循环工作。我不知道如果我得到一整条线该怎么办
我知道当用户输入一个空格表示一个新词,当他们输入一个句点表示结束。我很难理解您的代码。(看起来你想同时用两种方法来做这件事?)不管怎样,我相信我能理解你的问题。以下是一个可编译和可运行的示例:
import java.util.Scanner;
public class PigLatin
{
public static void main(String[] args)
{
System.out.print("Enter a line of text: ");
Scanner keyboard = new Scanner(System.in);
String text = keyboard.nextLine();
System.out.println("\nInput: " + text);
System.out.print("Output: ");
if (text != null && text.length() > 0)
{
int i = 0;
// this iterates through the whole string, stopping at a period or
// the end of the string, whichever is closer
while (i < text.length() && text.charAt(i) != '.')
{
// these three variables only exist in this code block,
// so they will be re-initialized to these values
// each time this while loop is executed
char first = '\0'; // don't worry about this, I just use this value as a default initializer
boolean isFirst = true;
boolean firstIsVowel = false;
// each iteration of this while loop should be a word, since it
// stops iterating when a space is encountered
while (i < text.length()
&& text.charAt(i) != ' '
&& text.charAt(i) != '.')
{
// this is the first letter in this word
if (isFirst)
{
first = text.charAt(i);
// deal with words starting in vowels
if (first == 'a' || first == 'A' || first == 'e' || first == 'E'
|| first == 'i' || first == 'I' || first == 'o' || first == 'O'
|| first == 'u' || first == 'U')
{
System.out.print(first);
firstIsVowel = true;
}
// make sure we don't read another character as the first
// character in this word
isFirst = false;
}
else
{
System.out.print(text.charAt(i));
}
i++;
}
if (firstIsVowel)
{
System.out.print("-tay ");
}
else if (first != '\0')
{
System.out.print("-" + first + "ay ");
}
i++;
}
System.out.print('\n'); //for clean otuput
}
}
}
import java.util.Scanner;
公共级拉丁语
{
公共静态void main(字符串[]args)
{
System.out.print(“输入一行文本:”);
扫描仪键盘=新扫描仪(System.in);
字符串文本=键盘.nextLine();
System.out.println(“\nInput:+text”);
系统输出打印(“输出:”);
if(text!=null&&text.length()>0)
{
int i=0;
//它遍历整个字符串,在一个句点或
//字符串的末尾,以较近者为准
而(i
这里有一些评论可以帮助你理解我的逻辑。这几乎肯定不是最有效的方法(即使有你的限制),因为我只是把它作为你可以使用的逻辑类型的一个例子。你可以把它分解成单词,然后在你点击空格或句点时处理当前单词:
System.out.print("Enter a line of text: ");
Scanner keyboard = new Scanner(System.in);
String text = keyboard.nextLine();
System.out.println("\nInput: " + text);
System.out.print("Output: ");
String curWord = "";
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == ' ' || text.charAt(i) == '.') {
if (curWord.charAt(0) == 'a' || curWord.charAt(0) == 'e' ||
curWord.charAt(0) == 'i' || curWord.charAt(0) == 'o' ||
curWord.charAt(0) == 'u') {
System.out.print(curWord + "-way ");
} else {
for (int j = 1; j < curWord.length(); j++) {
System.out.print(curWord.charAt(j);
}
System.out.print("-" + curWord.charAt(0) + "ay ");
//System.out.print(curWord.substring(1)+"-"+curWord.charAt(0)+"ay ");
}
curWord = "";
} else {
curWord += text.charAt(i);
}
}
System.out.print(“输入一行文本:”);
扫描仪键盘=新扫描仪(System.in);
字符串文本=键盘.nextLine();
System.out.println(“\nInput:+text”);
系统输出打印(“输出:”);
字符串curWord=“”;
对于(int i=0;i
对我来说似乎是一个很好的家庭作业问题:)我不能使用。子字符串,但这是我的第一个想法,谢谢你当时不必使用子字符串。查看更新的答案。我尝试了这个,效果很好!这真的很有道理。然而,我有一个问题,当程序编译时,为什么它会运行,但在最后也有一个错误?我在线程main中得到一个异常
错误,但它仍然输出答案啊,是的,这是我的错误。现在修好了。我可能使用过也可能没有使用过非常彻底的测试过程,即仅使用字符串进行测试。这是一个测试。
。。。看起来结尾没有句点的字符串失败了。无论如何,导致异常的原因是i
最初在test.charAt(i)!=”之后代码>在内部while循环中。按照它们现在的顺序,它将工作,因为while语句将在i
失败时立即失败,这意味着text.charAt(i)
(导致异常的方法调用)从未被调用。啊,是的,这是有意义的,幸运的是,假设用户在末尾放置了一个句点是安全的,但这样做要彻底得多,非常感谢。我现在要自己再试一次