【问题标题】:Pig Latin Translator spitting out multiple lines? C#猪拉丁语翻译器吐出多行? C#
【发布时间】:2016-03-26 10:42:49
【问题描述】:

所以我有一个支持多个单词的 Pig 拉丁语翻译器。但是每当我输入一些单词时(例如,我们将只使用“香蕉苹果剪巧克力西奥多火车”。)它会正确吐出翻译的单词,但它会重复!这是我的代码:

namespace Pig_Latin_Translator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    List<string> vowels = new List<string>();
    List<string> specials = new List<string>();

    private void TranslateButton_Click(object sender, EventArgs e)
    {
        String[] parts = TranslateBox.Text.Split();
        foreach (string s in specials)
        {
            if (TranslateBox.Text.Contains(s) || TranslateBox.Text == "\"")
            {
                TranslateOutput.Text = "";
                MessageBox.Show("No Special Characters!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                break;
            }
            else
            {
                 foreach (String part in parts)
                {
                    foreach (String v in vowels)
                    {
                        if (part.Substring(0, 1) == v)
                        {
                            TranslateOutput.Text = TranslateOutput.Text + " " + part + "ay";
                            break;
                        }
                        else
                        {
                            if (part.Substring(0, 2) == "sh" || part.Substring(0, 2) == "ch" || part.Substring(0, 2) == "th" || part.Substring(0, 2) == "tr")
                            {
                                string SwitchP = part.Substring(2) + part.Substring(0, 2);
                                TranslateOutput.Text = TranslateOutput.Text + " " + SwitchP + "ay";
                                break;
                            }
                            else
                            {
                                string Switch = part.Substring(1) + part.Substring(0, 1);
                                TranslateOutput.Text = TranslateOutput.Text + " " + Switch + "ay";
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        vowels.Add("a");
        vowels.Add("e");
        vowels.Add("i");
        vowels.Add("o");
        vowels.Add("u");

        specials.Add("`");
        specials.Add("1");
        specials.Add("2");
        specials.Add("3");
        specials.Add("4");
        specials.Add("5");
        specials.Add("6");
        specials.Add("7");
        specials.Add("8");
        specials.Add("9");
        specials.Add("0");
        specials.Add("-");
        specials.Add("=");
        specials.Add("[");
        specials.Add("]");
        specials.Add(@"\");
        specials.Add(";");
        specials.Add("'");
        specials.Add(",");
        specials.Add(".");
        specials.Add("/");

        specials.Add("~");
        specials.Add("!");
        specials.Add("@");
        specials.Add("#");
        specials.Add("$");
        specials.Add("%");
        specials.Add("^");
        specials.Add("&");
        specials.Add("*");
        specials.Add("(");
        specials.Add(")");
        specials.Add("_");
        specials.Add("+");
        specials.Add("{");
        specials.Add("}");
        specials.Add("|");
        specials.Add(":");
        specials.Add("\"");
        specials.Add("<");
        specials.Add(">");
        specials.Add("?");
    }

    private void AboutButton_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Pig Latin is a fake language. It works by taking the first letter (Or two if it's a pair like 'th' or 'ch') and bringing it to the end, unless the first letter is a vowel. Then add 'ay' to the end. So 'bus' becomes 'usbay', 'thank' becomes 'ankthay' and 'apple' becomes 'appleay'.", "About:", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

}

如果您输入“香蕉苹果剪巧克力西奥多火车”会输出:

“ananabay appleay earshay ocolatechay heodoreTay aintray”重复超过 10 次。

顺便说一句:对不起,如果你不能回答,因为我知道有很多代码。但这没关系,因为它仍然有用。只是它不应该发生并让我紧张。而且我知道还有很多故障和更多工作要做,但我想先解决这个问题。

【问题讨论】:

    标签: c# winforms translators


    【解决方案1】:

    您将代码嵌套在不应嵌套的两个循环中

    foreach (string s in specials)
    

    foreach (String v in vowels)
    

    你的break 声明让你摆脱了其中一个的麻烦,但不是另一个。

    如果您使用 .Any(...) 谓词,您可以完全避免这些循环。

    您的代码如下所示:

    private void TranslateButton_Click(object sender, EventArgs e)
    {
        TranslateOutput.Text = "";
        if (specials.Any(s => TranslateBox.Text.Contains(s)))
        {
            MessageBox.Show("No Special Characters!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else
        {
            String[] parts = TranslateBox.Text.Split();
            foreach (var part in parts)
            {
                var index = 1;
                if (vowels.Any(v => part.Substring(0, 1).ToLower() == v))
                {
                    index = 0;
                }
                else if (new [] { "sh", "ch", "th", "tr", }.Contains(part.Substring(0, 2).ToLower()))
                {
                    index = 2;
                }
                TranslateOutput.Text += " " + part.Substring(index) + part.Substring(0, index);
            }
        }
        TranslateOutput.Text = TranslateOutput.Text.TrimEnd();
    }
    

    这会将其归结为您实际需要的 foreach 循环。

    你也可以让你的代码初始化vowelsspecials到这个:

    vowels.AddRange("aeiou".Select(x => x.ToString()));
    specials.AddRange(@"`1234567890-=[]\;',./~!@#$%^&*()_+{}|:""<>?".Select(x => x.ToString()));
    

    【讨论】:

    • 谢谢!这真的有帮助。
    • @NumNumDude - 如果它真的有帮助,那么您可以投票,甚至可以将其标记为已接受的答案。 :-)
    【解决方案2】:

    您正在为每个特殊字符迭代一次您的单词。您的foreach 用于检查您的文字并翻译在您的foreach 内,以检查文本框是否包含任何特殊字符。

    换句话说,您将对每个特殊字符进行一次翻译。

    您需要将您的 foreach (String part in parts) 移出您的 foreach (string s in specials)

    【讨论】:

    • 如果您不确定是否有答案 - 将其发布为 cmets。否则,请避免使用诸如“希望你喜欢它”或“试试这个”之类的绒毛文字。
    【解决方案3】:

    您的循环中有一点逻辑问题。

    你的外循环:

    foreach( string s in specials ) {
    

    ...正在遍历您的特殊字符列表中的所有 42 个字符。

    你的内循环

    foreach( String part in parts ) {
    

    ...然后执行 42 次。因此,对于您的六个单词示例,您实际上进行了 252 次猪拉丁语转换。

    如果您从外部提取内部循环,您的结果会更好。像这样:

    foreach( string s in specials ) {
        if( TranslateBox.Text.Contains( s ) || TranslateBox.Text == "\"" ) {
            TranslateOutput.Text = "";
            MessageBox.Show( "No Special Characters!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning );
            return;
        }
    }
    
    String[] parts = TranslateBox.Text.Split();
    foreach( String part in parts ) {
        foreach( String v in vowels ) {
            if( part.Substring( 0, 1 ) == v ) {
                TranslateOutput.Text = TranslateOutput.Text + " " + part + "ay";
                break;
            }
            else {
                if( part.Substring( 0, 2 ) == "sh" || part.Substring( 0, 2 ) == "ch" || part.Substring( 0, 2 ) == "th" || part.Substring( 0, 2 ) == "tr" ) {
                    string SwitchP = part.Substring( 2 ) + part.Substring( 0, 2 );
                    TranslateOutput.Text = TranslateOutput.Text + " " + SwitchP + "ay";
                    break;
                }
                else {
                    string Switch = part.Substring( 1 ) + part.Substring( 0, 1 );
                    TranslateOutput.Text = TranslateOutput.Text + " " + Switch + "ay";
                    break;
                }
            }
        }
    }
    

    更简洁的实现是:

    private void TranslateButton_Click( object sender, EventArgs e )
    {
        char[] specials = "`1234567890-=[]\";',./~!@#$%^&*()_+{}|:\\<>?".ToArray();
        char[] vowels = "aeiou".ToArray();
    
        TranslateOutput.Text = String.Empty;
    
        if( TranslateBox.Text.IndexOfAny( specials ) > -1 ) {
            MessageBox.Show( "No Special Characters!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning );
            return;
        }
    
        String[] parts = TranslateBox.Text.Split();
        foreach( String part in parts ) {
            int firstVowel = part.IndexOfAny( vowels );
            if( firstVowel > 0 ) {
                TranslateOutput.Text += part.Substring( firstVowel ) + part.Substring( 0, firstVowel ) + "ay ";
            }
            else {
                TranslateOutput.Text += part + "ay ";
            }
        }
    
        TranslateOutput.Text = TranslateOutput.Text.TrimEnd();
    }
    

    在本例中,我为特殊字符和元音创建了两个字符数组。然后我可以利用框架的IndexOfAny 方法来搜索数组中的任何字符并返回第一次出现的索引。这将在第一个循环中找到第一个特殊字符(如果有),在第二个循环中找到第一个元音。一旦我从单词中获得了字符索引,我就可以将单词解析成猪拉丁语。请注意,我正在检查零作为元音索引,因为在猪拉丁语中,前导元音保持在原位,并且“ay”只是附加到单词的末尾。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多