C#/ Mysql测验只有5个问题

时间:2015-07-02 09:35:01

标签: c# mysql

我的代码有点问题,希望你能帮助我。

我试图写一个在表格上显示问题的测验, 您可以通过单击5个答案按钮之一来回答它们。 问题存储在Mysql-Database中。

在每场比赛中,球员必须回答5个问题, 这就是问题所在:

在5个问题之后,它不会进入分数页。

如何解决此问题?

这是我的尝试:

     public int richtig;

        int Counter = 0;
        string Loesung, AntwortA_value, AntwortB_value, AntwortC_value, AntwortD_value, AntwortE_value;

        string cmdText = "SELECT fragen,A,B,C,D,E,Loesungen from Fragen order by Rand() Limit 1;";
        string MyConString = "SERVER=localhost;DATABASE=quiz;UID=xxx;PASSWORD=xxx;";

        public Fragen()
        {
            InitializeComponent();
            Questions();

        }

        private void Antworten_Click(object sender, EventArgs e)
        {
            if (Counter != 4)
            {
                Questions();
                Counter++;
            }
            else
            {

            }


        }

        private void Questions()
        {
            if (Counter != 5)
            {
                using (MySqlConnection connection = new MySqlConnection(MyConString))
                {
                    try
                    {
                        MySqlCommand cmd = new MySqlCommand(cmdText, connection);
                        connection.Open();
                        MySqlDataReader reader = cmd.ExecuteReader();

                        while (reader.Read())
                        {
                            FragenTitel.Text = (reader[0].ToString());
                            AntwortA.Text = (reader[1].ToString());
                            AntwortB.Text = (reader[2].ToString());
                            AntwortC.Text = (reader[3].ToString());
                            AntwortD.Text = (reader[4].ToString());
                            AntwortE.Text = (reader[5].ToString());
                            Loesung = (reader[6].ToString());

                            AntwortA_value = (reader[1].ToString());
                            AntwortB_value = (reader[2].ToString());
                            AntwortC_value = (reader[3].ToString());
                            AntwortD_value = (reader[4].ToString());
                            AntwortE_value = (reader[5].ToString());
                        }
                        reader.Close();
                        connection.Close();

                        //lblError.Text = "Data Saved";
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("not entered");
                        //lblError.Text = ex.Message;
                    }

                }
            }
            else
            {
                Auswertungen Aw = new Auswertungen();
                Aw.Show();
            }

            Counter = richtig;
        }

        #region Antwortbuttons

        private void AntwortButtonA_Click(object sender, EventArgs e)
        {
            if (AntwortA_value == Loesung)
            {
                MessageBox.Show("Richtig");
                Counter++;
                richtig++;
                Questions();
            }
            else
            {
                MessageBox.Show("Falsch");
                Counter++;
                Questions();
            }
        }

        private void AntwortButtonB_Click(object sender, EventArgs e)
        {
            if (AntwortB_value == Loesung)
            {
                MessageBox.Show("Richtig");
                Counter++;
                richtig++;
                Questions();
            }
            else
            {
                MessageBox.Show("Falsch");
                Counter++;
                Questions();
            }
        }

        private void AntwortButtonC_Click(object sender, EventArgs e)
        {
            if (AntwortB_value == Loesung)
            {
                MessageBox.Show("Richtig");
                Counter++;
                richtig++;
                Questions();
            }
            else
            {
                MessageBox.Show("Falsch");
                Counter++;
                Questions();
            }
        }

        private void AntwortButtonD_Click(object sender, EventArgs e)
        {
            if (AntwortD_value == Loesung)
            {
                MessageBox.Show("Richtig");
                Counter++;
                richtig++;
                Questions();
            }
            else
            {
                MessageBox.Show("Falsch");
                Counter++;
                Questions();
            }
        }

        private void AntwortButtonE_Click(object sender, EventArgs e)
        {
            if (AntwortE_value == Loesung)
            {
                MessageBox.Show("Richtig");
                Counter++;
                richtig++;
                Questions();
            }
            else
            {
                MessageBox.Show("Falsch");
                Counter++;
                Questions();
            }
        #endregion


        }

        private void Score_uebername()
        {
          //  richtig = Auswertungen.Punktzahl;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

问题在于:

 if (Counter != 4)
            {
                Questions();
                Counter++;
            }
            else
            {

            }

这种代码的和平永远不会让计数器达到5,这意味着这个条件:

if (Counter != 5)
            {
               ....
            }
            else <--this condition is never met because Counter never gets to 5
            {
                Auswertungen Aw = new Auswertungen();
                Aw.Show();
            }

一旦用户回答了5个Q(计数器= 4),程序就会在一个空的else块中空闲..