我一直在研究这个Pig Latin Translator,我刚刚完成,除了这两个(相同的)while循环没有按预期执行的事实。当我试图用一个短语来翻译时,比如“我的名字就是”,它应该是“yMay amenay isway”。问题是指示的循环无限地执行是出于我不知道的原因。否则我已经测试以确保此代码正常工作。我不知道如何让它发挥作用。有什么想法吗?非常感谢!
import java.io.*;
import java.util.*;
import java.util.Arrays;
public class PigLatin
{
public static void main (String[] args)
{
System.out.print("Please enter a phrase to translate: ");
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
String[] words = str.split("\\s+");
int period = words.length;
int spaces = (period - 1);
String[] word = Arrays.copyOfRange(words,0,spaces);
for (int i = 0; i < word.length; i++)
{
String a = word[i].substring(0,1);
int b = a.length();
int c = word[i].length();
while (b <= 4) //start of thought problem
{
if (!(a.contains("a") || a.contains("e") || a.contains("i") || a.contains("o") || a.contains("u")))
{
a = word[i].substring(0,b);
b = b + 1;
}
} // end of thought problem
if (word[i].startsWith("a") || word[i].startsWith("e") || word[i].startsWith("i") || word[i].startsWith("o") || word[i].startsWith("u"))
{
System.out.print(word[i] + "way");
}
else if (!(a.contains("a") || a.contains("e") || a.contains("i") || a.contains("o") || a.contains("u")))
{
String answer = word[i].substring(b,c);
System.out.print(answer + a + "ay");
}
System.out.print(" ");
}
String end = "";
for (String endArray: Arrays.copyOfRange(words,spaces,period))
{
end = end + endArray;
}
String z = end.substring(0,1);
int x = z.length();
int y = end.length();
while (x <= 4) //start of thought problem
{
if (!(z.contains("a") || z.contains("e") || z.contains("i") || z.contains("o") || z.contains("u")))
{
z = end.substring(0,x);
x = x + 1;
}
} //end of thought problem
if (end.startsWith("a") || end.startsWith("e") || end.startsWith("i") || end.startsWith("o") || end.startsWith("u"))
{
System.out.print(end + "way");
}
else if (!(z.contains("a") || z.contains("e") || z.contains("i") || z.contains("o") || z.contains("u")))
{
String answer = end.substring(x,y);
System.out.print(answer + z + "ay");
}
System.out.print(".");
}
}
答案 0 :(得分:1)
您的代码格式很糟糕,在这里它被提取到方法中并重命名了一些变量。我还没有修复任何错误,可以扩展重命名。
import java.io.*;
import java.util.*;
import java.util.Arrays;
public class PigLatin
{
String[] GetWords()
{
System.out.print("Please enter a phrase to translate: ");
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
String[] words = str.split("\\s+");
int period = words.length;
int spaces = (period - 1);
return Arrays.copyOfRange(words,0,spaces);
}
bool ContainsVowel(String text)
{
return text.contains("a") || text.contains("e") || text.contains("i") || text.contains("o") || text .contains("u")
}
bool StartsWithVowel(String text)
{
return text.startsWith("a") || text.startsWith("e") || text.startsWith("i") || text.startsWith("o") || text.startsWith("u")
}
String PigLatin(String text)
{
String prefix = text.substring(0,1);
int b = a.length();
int c = text.length();
while (b <= 4) //start of thought problem
{
if (!ContainsVowel(prefix))
{
prefix= text.substring(0,b);
b = b + 1;
}
} // end of thought problem
if (StartsWithVowel(text)
{
return text + "way";
}
else if (!ContainsVowel(prefix))
{
String answer = text.substring(b,c);
return answer + prefix + "ay";
}
return " ";
}
public static void main (String[] args)
{
String[] words = GetWords();
for (int i = 0; i < words.length; i++)
{
String translation = PigLatin(words[i]);
System.out.print(translation + " ");
}
String end = "";
for (String endArray: Arrays.copyOfRange(words,spaces,period))
{
end = end + endArray;
}
String z = end.substring(0,1);
int x = z.length();
int y = end.length();
while (x <= 4) //start of thought problem
{
if (!ContainsVowel(z))
{
z = end.substring(0,x);
x = x + 1;
}
} //end of thought problem
if (StartsWithVowel(end))
{
System.out.print(end + "way");
}
else if (!ContainsVowel(z))
{
String answer = end.substring(x,y);
System.out.print(answer + z + "ay");
}
System.out.print(".");
}
}
这应该对你的逻辑有很大的帮助。它更具可读性,我可以开始理解你想要做的事情。
你可以改进很多。我要做的第一件事是将if (StartsWithVowel(end))
移到PigLatin(String text)
方法的开头。这样你就不会做额外的工作或根据一个错误的假设进入循环。
答案 1 :(得分:0)
如果您格式化代码会有所帮助
while (b <= 4) //start of thought problem
{
System.out.println("Watch this never end"); //Add this print line
if (!(a.contains("a") || a.contains("e") || a.contains("i") || a.contains("o") || a.contains("u")))
{
a = word[i].substring(0,b);
b = b + 1;
}
}
同样适用于
while (x <= 4) //start of thought problem
{
if (!(z.contains("a") || z.contains("e") || z.contains("i") || z.contains("o") || z.contains("u")))
{
z = end.substring(0,x);
x = x + 1;
}
}
正如你所看到的那样,如果你从未进入过if语句,你就永远不会离开这个循环。请尝试使用for循环。
for(int b = a.length(); b <=4; b++)