0

我一直在研究这个 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(".");
    }
}
4

3 回答 3

0

在两个循环中,如果if条件为false,则while循环变量永远不会递增。瞧!无限循环。

我没有对您的逻辑进行逆向工程,但您可能希望每次通过循环时都增加循环变量(b和),而不仅仅是在满足条件时。xif

于 2013-07-25T02:33:42.303 回答
0

可能发生的情况是该if语句的主体不会运行足够的时间以使while循环退出。:

if (!(z.contains("a") || z.contains("e") || z.contains("i") || z.contains("o") || z.contains("u")))

所以只要z包含一个元音,你就会有一个无限循环。

于 2013-07-25T02:34:54.397 回答
0
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;
        }

如果你将你x = x + 1;if语句移出你的 while 循环将被终止。

于 2013-07-25T04:02:29.723 回答