C#/Mysql测验只有5个问题
本文关键字:5个 问题 Mysql | 更新日期: 2023-09-27 18:00:43
我的代码有点问题,希望你能帮助我。
我正在写一个测验,在表格上显示问题,您可以通过单击"5个答案"按钮中的一个来回答这些问题。问题存储在Mysql数据库中。
在每个游戏中,玩家必须回答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;
}
}
}
问题就在这里:
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块中空闲。。