如何使用wir循环而不是开关案例语句以允许有多个问题数

发布于 2025-02-01 04:25:39 字数 2818 浏览 1 评论 0原文

我目前正在努力在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女皇必胜 2025-02-08 04:25:39
public string getValue(string columnName, string tableName, string refName, string refValue, string ref2Name, string ref2Value)
    {
        string serverIp = "localhost";
        string username = "root";
        string password = "Shared1203";
        string databaseName = "faquiz";

        string value = "";

        string dbConnectionString = string.Format("server={0};uid={1};pwd={2};database={3};", serverIp, username, password, databaseName);
        string query = "SELECT " + columnName + " FROM " + tableName + " WHERE " + refName + " = " + refValue + " AND " + ref2Name + " = " + ref2Value;

        var conn = new MySql.Data.MySqlClient.MySqlConnection(dbConnectionString);
        conn.Open();

        var cmd = new MySql.Data.MySqlClient.MySqlCommand(query, conn);
        var reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            var someValue = reader[columnName];

            value = someValue.ToString();
        }

        return value;
    }

没有收到错误

public string getValue(string columnName, string tableName, string refName, string refValue, string ref2Name, string ref2Value)
    {
        string serverIp = "localhost";
        string username = "root";
        string password = "Shared1203";
        string databaseName = "faquiz";

        string value = "";

        string dbConnectionString = string.Format("server={0};uid={1};pwd={2};database={3};", serverIp, username, password, databaseName);
        string query = "SELECT " + columnName + " FROM " + tableName + " WHERE " + refName + " = " + refValue + " AND " + ref2Name + " = " + ref2Value;

        var conn = new MySql.Data.MySqlClient.MySqlConnection(dbConnectionString);
        conn.Open();

        var cmd = new MySql.Data.MySqlClient.MySqlCommand(query, conn);
        var reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            var someValue = reader[columnName];

            value = someValue.ToString();
        }

        return value;
    }

No errors received

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文