如何使用wir循环而不是开关案例语句以允许有多个问题数
我目前正在努力在C#开发“测验”程序,我想提出的问题数量,以便将来可以轻松地更改它。以前,我使用了一个开关案例语句来运行一系列20个问题,并且每个语句中的代码几乎相同,除了我从mySQL中提取的值的参数。但是,当我尝试在一段时间内做同样的事情时,测验窗口不会弹出。这是在Visual Studio 2019上使用Web测验.NET模板构建的。请看一下屏幕截图,让我知道您是否看到可能导致测验不会在While循环中显示的任何错误,但确实显示在开关情况下。 screenShot 另外,此外,这是当我使用开关情况时会弹出的内容,但没有与while循环。我的代码:
private void askQuestion(int qnum)
{
int numOfRows = 20;
int questionNum = qnum;
while (questionNum <= numOfRows)
{
string num = "'" + qnum.ToString() + "'";
lblQuestion.Text = getValue("question", "quizquestions", "questionid", num, "questionid", num);
//lblQuestion.Text = "An alarm may be canceled by calling the monitoring station and giving them the correct passcode and / or password.";
button1.Text = getValue("answertext", "answertable", "answerid", "'1'", "questionid", num);
button2.Text = getValue("answertext", "answertable", "answerid", "'2'", "questionid", num);
button3.Text = getValue("answertext", "answertable", "answerid", "'3'", "questionid", num); ;
button4.Text = getValue("answertext", "answertable", "answerid", "'4'", "questionid", num); ;
scoreBox.Text = "Current Score: " + score + "/20";
currentQ.Text = "Current Question: " + questionNumber;
correctAnswer = Int32.Parse(getValue("correctid", "quizanswers", "questionid", num, "questionid", num));
questionNum++;
}
}
/*private void askQuestion(int qnum)
{
switch (qnum)
{
case 1:
//pictureBox1.Image = Properties.Resources.questions;
lblQuestion.Text = getValue("question", "quizquestions", "questionid", "'1'", "questionid", "1");
//lblQuestion.Text = "An alarm may be canceled by calling the monitoring station and giving them the correct passcode and / or password.";
button1.Text = getValue("answertext", "answertable", "answerid", "'1'", "questionid", "'1'");
button2.Text = getValue("answertext", "answertable", "answerid", "'2'", "questionid", "'1'");
button3.Visible = false;
button3.Text = "";
button4.Visible = false;
button4.Text = "";
scoreBox.Text = "Current Score: " + score +"/20";
currentQ.Text = "Current Question: " + questionNumber;
correctAnswer = Int32.Parse(getValue("correctid", "quizanswers", "questionid", "'1'", "questionid", "'1'"));
break;
case 2:...
I am currently working on developing a "quiz" program in C# and I want to make the number of questions it asks variable so it can be easily changed in the future. Previously, I used a switch case statement to run a series of 20 questions, and the code in each statement is virtually same except for the parameters for which values I pull from MySQL. However, when I try to do the same thing in a while loop, the quiz window does not pop up. This is built on Visual Studio 2019 using the Web Quiz .NET template. Please take a look at the screenshots and let me know if you see any errors that could be causing the quiz to not show up in the while loop but does show up in the switch case. Screenshot Also, here is What pops up when I use the switch case but doesnt with the while loop. My code:
private void askQuestion(int qnum)
{
int numOfRows = 20;
int questionNum = qnum;
while (questionNum <= numOfRows)
{
string num = "'" + qnum.ToString() + "'";
lblQuestion.Text = getValue("question", "quizquestions", "questionid", num, "questionid", num);
//lblQuestion.Text = "An alarm may be canceled by calling the monitoring station and giving them the correct passcode and / or password.";
button1.Text = getValue("answertext", "answertable", "answerid", "'1'", "questionid", num);
button2.Text = getValue("answertext", "answertable", "answerid", "'2'", "questionid", num);
button3.Text = getValue("answertext", "answertable", "answerid", "'3'", "questionid", num); ;
button4.Text = getValue("answertext", "answertable", "answerid", "'4'", "questionid", num); ;
scoreBox.Text = "Current Score: " + score + "/20";
currentQ.Text = "Current Question: " + questionNumber;
correctAnswer = Int32.Parse(getValue("correctid", "quizanswers", "questionid", num, "questionid", num));
questionNum++;
}
}
/*private void askQuestion(int qnum)
{
switch (qnum)
{
case 1:
//pictureBox1.Image = Properties.Resources.questions;
lblQuestion.Text = getValue("question", "quizquestions", "questionid", "'1'", "questionid", "1");
//lblQuestion.Text = "An alarm may be canceled by calling the monitoring station and giving them the correct passcode and / or password.";
button1.Text = getValue("answertext", "answertable", "answerid", "'1'", "questionid", "'1'");
button2.Text = getValue("answertext", "answertable", "answerid", "'2'", "questionid", "'1'");
button3.Visible = false;
button3.Text = "";
button4.Visible = false;
button4.Text = "";
scoreBox.Text = "Current Score: " + score +"/20";
currentQ.Text = "Current Question: " + questionNumber;
correctAnswer = Int32.Parse(getValue("correctid", "quizanswers", "questionid", "'1'", "questionid", "'1'"));
break;
case 2:...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有收到错误
No errors received