在测试问题中循环进行失败
本文关键字:失败 循环 测试 问题 | 更新日期: 2023-09-27 18:03:17
编辑 -我最初发布了我的代码的早期版本,现在更正代码
我有一个测试问题的数据库表,我在我的表格中显示,但是,当我得到表中的最后一个问题时,我得到以下错误:
IndexOutOfRangeException was unhandled
There is no row at position 5
我的表结构是这样的:
问题编号|问题|答案1 |答案2 |答案3 |答案4 |正确答案
下面是我的代码:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class QuizQuestions : Form
{
public WindowsAnalysisQuiz()
{
InitializeComponent();
}
//int questionNumber;
String correctAnswer;
private void WindowsAnalysisQuiz_Load(object sender, EventArgs e)
{
//declare connection string using windows security
string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:''Users''Hanna''Desktop''QuizQuestions.accdb";
//declare Connection, command and other related objects
OleDbConnection conGet = new OleDbConnection(cnString);
OleDbCommand cmdGet = new OleDbCommand();
//try
//{
//open connection
conGet.Open();
//String correctAnswer;
cmdGet.CommandType = CommandType.Text;
cmdGet.Connection = conGet;
cmdGet.CommandText = "SELECT * FROM QuizQuestions ORDER BY rnd()";
OleDbDataReader reader = cmdGet.ExecuteReader();
reader.Read();
label1.Text = reader["Question"].ToString();
radioButton1.Text = reader["Answer 1"].ToString();
radioButton2.Text = reader["Answer 2"].ToString();
radioButton3.Text = reader["Answer 3"].ToString();
radioButton4.Text = reader["Answer 4"].ToString();
correctAnswer = reader["Correct Answer"].ToString();
//questionNumber = 0;
conGet.Close();
}
private void btnNextQuestion_Click(object sender, EventArgs e)
{
String cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:''Users''Hannah''Desktop''QuizQuestions.accdb";
int questionNumber = 0;
//declare Connection, command and other related objects
OleDbConnection conGet = new OleDbConnection(cnString);
OleDbCommand cmdGet = new OleDbCommand();
//try
{
//open connection
conGet.Open();
cmdGet.CommandType = CommandType.Text;
cmdGet.Connection = conGet;
cmdGet.CommandText = "SELECT * FROM QuizQuestions ORDER BY rnd()"; // select all columns in all rows
OleDbDataReader reader = cmdGet.ExecuteReader();
reader.Read();
String chosenAnswer = "";
//int chosenCorrectly = 0;
if (radioButton1.Checked)
{
chosenAnswer = reader["Answer 1"].ToString();
}
else if (radioButton2.Checked)
{
chosenAnswer = reader["Answer 2"].ToString();
}
else if (radioButton3.Checked)
{
chosenAnswer = reader["Answer 3"].ToString();
}
else
{
chosenAnswer = reader["Answer 4"].ToString();
}
if (chosenAnswer == reader["Correct Answer"].ToString())
{
labelQuestion.Text = table.Rows[questionNumber]["Question"].ToString();
//and show possible answers:
radioButton1.Text = table.Rows[questionNumber]["Answer 1"].ToString();
radioButton2.Text = table.Rows[questionNumber]["Answer 2"].ToString();
radioButton3.Text = table.Rows[questionNumber]["Answer 3"].ToString();
radioButton4.Text = table.Rows[questionNumber]["Answer 4"].ToString();
correctAnswer = table.Rows[questionNumber]["Correct Answer"].ToString();
questionNumber++; //got to next question!
}
else
{
MessageBox.Show("That is not the correct answer");
}
}
}
}
}
我要做的是得到一个for循环它将列出数据库中的问题但同时又能让我在回答完最后一个问题后将正确回答的所有答案加起来,当然,不会得到我得到的错误
我认为questionNumber
的起始值需要为0,因为Rows是基于零的,否则table.Rows[questionNumber]
将在最后一行失败。
您应该将questionNumber初始值设置为0,因为行是从零开始的。获取pagload中的问题数并将其设为另一个变量int questionCount
in buttonClick用questionNumber= (questionNumber++%questionCount);
问题结束后会转到开始。
每次选择正确答案时,btnGoToNextOne_Click中的代码都会无条件地增加questionNumber (questionnumber++)。当正确选择最后一个问题时,questionNumber索引超出范围,因此这可能是代码中其他地方的问题。
另一个是,在Form1_Load中,您设置questionNumber = 1,这假设您在数据库中至少有2个问题。除非您出于某种原因将questionNumber设置为1,否则它可能应该设置为0(因为c#中的索引是从零开始的)。
也有很多需要的方式,你正在做的事情在这段代码,重复字符串,OleDbX…X对象应该包装在using(OleDbX…X){}中,以便它们被正确处理;为什么DataTable表不是一个本地变量,我不知道…
如果这是最后一行,你应该告诉编译器结束,也就是说你应该添加这个。
if (chosenAnswer == reader["Correct Answer"].ToString() &&
questionNumber <= tables[your table].rows.count())