1

簡単なクイズ形式のフォーム アプリケーションの作成を伴う課題を行っています。ただし、プログラムを実行するたびに、複数の質問に対して最初の回答のみが表示され、その理由を一生理解できません。

これはコンストラクタです:

MultipleChoice dlg =
    new MultipleChoice(
        new Question("What is the capital of Zimbabwe?",
            new Answer("Paris", false),
            new Answer("Washington D.C.", false),
            new Answer("Harare", true),
            new Answer("Cairo", false),
            new Answer("N'Djamena", false)));
if (dlg.ShowDialog() == DialogResult.OK)
{
   if (dlg.Correct) MessageBox.Show("You got something right!");
   else MessageBox.Show("You couldn't be more wrong");
}

そして、これは質問フォームのコードです:

private Question Q;
public MultipleChoice (Question q)
{
    Q = q;
    InitializeComponent();
    textPrompt.Text = Q.Prompt;
    if (Q.A != null)
    {
        radioA.Text = Q.A.Prompt;
    }
    else radioA.Hide();
    if (Q.B != null)
    {
        radioB.Text = Q.B.Prompt;
    }
    radioB.Hide();
    if (Q.C != null)
    {
        radioC.Text = Q.C.Prompt;
    }
    radioC.Hide();
    if (Q.D != null)
    {
        radioD.Text = Q.D.Prompt;
    }
    radioD.Hide();
    if (Q.E != null)
    {
        radioE.Text = Q.E.Prompt;
    }
    radioE.Hide();
}
public bool Correct
{
    get
    {
        if (Q == null) return false;
        if (Q.A != null && Q.A.Correct && radioA.Checked) return true;
        if (Q.B != null && Q.B.Correct && radioB.Checked) return true;
        if (Q.C != null && Q.C.Correct && radioC.Checked) return true;
        if (Q.D != null && Q.D.Correct && radioD.Checked) return true;
        if (Q.E != null && Q.E.Correct && radioE.Checked) return true;
        return false;
    }
}

どこで間違ったのですか?

4

1 に答える 1

3

elseの後にオプションはありませんA:

if (Q.B != null) 
{ 
    radioB.Text = Q.B.Prompt; 
} 
radioB.Hide();  //This is **always** going to be called - hiding radioB :)

次のようにする必要があります。

if (Q.B != null) 
    radioB.Text = Q.B.Prompt; 
else
    radioB.Hide(); 
于 2012-07-03T21:21:22.203 に答える