0

グローバル文字列変数「word」があります。

    string word = "";
    List<Label> labels = new List<Label>();
    int amount = 0;

その単語は、テキスト ドキュメント (改行区切り) を解析することによって、次の 2 つの関数で定義/割り当てられます。

    void MakeLabels()
    {
        word = GetRandomWord();
        char[] chars = word.ToCharArray();
        ...
    }


    string GetRandomWord()
    {

        System.IO.StreamReader myFile = new System.IO.StreamReader(...);
        string myString = myFile.ReadToEnd();
        string[] words = myString.Split('\n');
        Random ran = new Random();
        return words[ran.Next(0, words.Length - 1)];
    }

最後に、テキストボックスの内容を「単語」変数に対して検証するイベントです。

    private void button2_Click(object sender, EventArgs e)
    {
        if (textBox2.Text == word)
        {
            MessageBox.Show(...);
        }
        else
        {
            MessageBox.Show(...);
            textBox2.Text = "";
            textBox1.Focus();
            Reset();
        }

私が抱えている問題は、textBox2 が「word」に相当する場合でも、else ステートメントに関連する MessageBox を受け取っていることです。「\ n」を運ぶ「単語」変数に関係していると思います。つまり、textBox2.Text = apple および word = apple\n であるため、2 つの変数は同等ではありません。助言がありますか?

4

4 に答える 4

0

問題が文字列の末尾にある改行であることが確実な場合は、String.Trim()メソッドを確認する必要があります。

于 2013-11-12T16:59:52.183 に答える
0
 string GetRandomWord()
  {

    string[] linse=  System .IO.File .ReadAllLines (......) ;
      string mlines = "";
        foreach (string line in linse)
          {
             if (line.Trim() != "")
                if(mlines=="")
                    mlines = line;
                else 
                mlines = mlines +"\n"+ line;
          }
        string[] words = mlines. Split('\n');

 Random ran = new Random();
    return words[ran.Next(0, words.Length - 1)];
}
于 2013-11-12T18:03:57.023 に答える