2012-04-19 5 views
1

質問のテストを表示するC#プログラムを作成しています。 テストには10​​の質問があります。 しかし、プログラムは、私は10 questions.Itがオンになった後、クイズが停止しないクイズを解決するたびに、10個の質問で質問の集まりが増加する

private void newToolStripMenuItem_Click (object sender, EventArgs e) 

の助けを借りて、新しいクイズを選択した場合と同じアルゴリズムに従うことを望んでいない、それはで質問を繰り返し、一定の数の質問がブロックされます。

私はコードをステップ、と私は見た:

  • を私は初めてのクイズ解決する場合:私は2番目の時間のためにそれを解決するとquestions.Count=10;
  • を:questions.Count=20;
  • 私が解決したときにそれは3回目です:questions.Count=30;

私はどのように質問しますか?questions.Count=10をどうすればいいですか?教えてください。ここで

は私のコードです:

public partial class Form3 : Form 
{ 
    int nr; 
    Collection<question> questions = new Collection<question>(); 

    public class question 
    { 
     public bool displayed = false; 
     public string text; 
    } 


    //when I press a button from MenuStrip my quiz begin 
    private void newToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     nr = 1; 
     button1.Enabled = true;//it's the next_question button 

     StreamReader sr = new StreamReader("quiz.txt"); 
     while (!sr.EndOfStream) 
     { 
      question i = new question(); 
      questions.Add(i); 
     } 
     sr.Close(); 
     int x = r.Next(questions.Count); 
     textBox1.Text = questionText; 
     questions[x].displayed = true; 
     current_question=x; 
     } 
} 

私は

 private void newToolStripMenuItem_Click(object sender, EventArgs e) 
の最初にこれを入れて、私は解決するすべてのクイズのため

 Collection<question> questions = new Collection<question>() 

を作成しようとしたことを追加します

または現在のクイズの最後に:

 if (nr >= questions.Count){ //here } 

これらの変更により、10個の質問でコレクションが増加することはありません。 ありがとうございました!

答えて

1

newToolStripMenuItem_Click方法の開始時に

questions = new Collection<question>(); 

を置くか、あなたは新しいローカル変数を作成しているので、その前にタイプCollection<question>を含んでいませんquestion.Clear();

を使用しています。

+0

はそんなにありがとうを!できます! – Bogdan

2

Clearメソッドの呼び出し追加:そうのよう

questions.Clear(); 

private void newToolStripMenuItem_Click(object sender, EventArgs e) 
{ nr = 1; 
    button1.Enabled = true;//it's the next_question button 
    questions.Clear(); 

    StreamReader sr = new StreamReader("quiz.txt"); 
    while (!sr.EndOfStream) 
    { 
     question i = new question(); 
     questions.Add(i); 
    } 
    sr.Close(); 
    int x = r.Next(questions.Count); 
    textBox1.Text = questionText; 
    questions[x].displayed = true; 
    current_question=x; 

    } 
+0

ありがとうございました!できます! – Bogdan

関連する問題