PigLatin翻译

时间:2014-01-11 11:42:40

标签: c# foreach

我将创建一个程序,将英语单词翻译成Pig Latin ...我在下面找到的代码的问题是,结果中报告的数组的最后一个索引中的唯一单词?有没有人看到错误?

提前致谢

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnTrans_Click( object sender, EventArgs e )
    {

        string engWordText = engWord.Text.ToString();
        string let1;
        string restLet;
        int position;
        string pigLatin = "";
        string vokal = "AEIOUaeiou";



         // split the sentence into individual words
         //string[] words = engWordText.Split(' ');
         string[] transWord = engWordText.Split(' ');

         // translate each word into pig latin
         foreach (string word in transWord)
         {
                 // check for empty TextBox
                 try
                 {
                     let1 = word.Substring(0, 1);
                     restLet = word.Substring(1, word.Length - 1);
                     position = vokal.IndexOf(let1);

                     if (position == -1)
                     {
                         pigLatin = restLet + let1 + "ay";
                     }
                     else
                     {
                         pigLatin = word + "way";
                     }

                     // display the translation
                     latinInput.Text = pigLatin.ToString();
                     engWord.Clear();

                 }
                 catch (System.ArgumentOutOfRangeException)
                 {
                     MessageBox.Show("Du måste skriva in ett engelskt ord", "PigLatin",
                     MessageBoxButtons.OK, MessageBoxIcon.Error);
                 }

         }


         } // end method translateButton_Click

         // pressing enter is the same as clicking the Translate Button
         private void engWordText_KeyDown( object sender, KeyEventArgs e )
         {
         // allow user to press enter in TextBox
         if ( e.KeyCode == Keys.Enter )
         btnTrans_Click( sender, e );
         } // end method inputTextBox_KeyDown


         } // end class PigLatinForm

1 个答案:

答案 0 :(得分:3)

您在每个循环结束时将pigLatin的值分配给文本框的Text属性,这意味着它只会分配给它的最后一个值。试试这个:

 List<string> plWords = new List<string>();         
 // translate each word into pig latin
 foreach (string word in transWord)
 {
         // check for empty TextBox
         try
         {
             let1 = word[0];
             restLet = word.Substring(1, word.Length - 1);

             if (!vokal.Contains(let1))
             {
                 pigLatin = restLet + let1 + "ay";
             }
             else
             {
                 pigLatin = word + "way";
             }

             plWords.Add(pigLatin);
         }
         catch (System.ArgumentOutOfRangeException)
         {
             MessageBox.Show("Du måste skriva in ett engelskt ord", "PigLatin",
             MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
 }

 engWord.Clear();    
 latinInput.Text = string.Join(" ", plWords.ToArray());

作为一个额外的奖励,这里是你如何使用Linq使这个操作更清洁:

private static string MakePigLatin(string word)
{
    const string vowels = "AEIOUaeiou";

    char let1 = word[0];
    string restLet = word.Substring(1, word.Length - 1);

    return vowels.Contains(let1) ? word + "way" : restLet + let1 + "ay";
}

private void btnTrans_Click( object sender, EventArgs e )
{
     var plWords = engWord.Text
                          .Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries)
                          .Select(MakePigLatin);

     latinInput.Text = string.Join(" ", plWords);
     engWord.Clear();    
}