-5

我一直在研究这个 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

2 回答 2

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)。这样你就不会做额外的工作或基于一个错误的假设进入循环。

于 2013-07-24T17:14:10.437 回答
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 语句,那么你永远不会离开这个 while 循环。尝试改用 for 循环。

for(int b = a.length(); b <=4; b++)
于 2013-07-24T17:12:55.660 回答