使用 Visual Studio 进行测验

Posted

技术标签:

【中文标题】使用 Visual Studio 进行测验【英文标题】:Quiz using Visual Studio 【发布时间】:2022-01-19 14:36:50 【问题描述】:

我想用 Visual Studio 做一个关于我的小测验。我的目标是每个问题有 10 个问题和 4 个选择。我尝试堆叠按钮,然后在不同时间更改 10 个标签,它变得有点复杂......使用数组或/和循环我怎样才能提高效率?这就是我所拥有的

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        Form1.Visible = False
        Button2.Visible = False
        Button3.Visible = False
        Button4.Visible = False
        Button5.Visible = False
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If RadioButton4.Checked = True Then
            Label3.Text = "1/10"
            Label4.Text = "Nice! That was Correct!
Great job!"
            Label4.ForeColor = Color.LimeGreen
        End If
        If Label3.Text = "1/10" Then
            Button2.Visible = True
        End If
        If RadioButton1.Checked = True Or RadioButton2.Checked = True Or RadioButton3.Checked = True Then
            Label4.ForeColor = Color.DarkRed
            Label4.Text = "Oh no! that was incorrect!
Try Again!"
        End If
        Button2.Text = "next"
    End Sub
    Private Sub Button2_click(sender As Object, e As EventArgs) Handles Button2.Click
        Label1.Text = "What is Luke Lopez's favorite color?"
        RadioButton4.Checked = False
        RadioButton1.Text = "Purple"
        RadioButton2.Text = "Burgundy"
        RadioButton3.Text = "Turqoise"
        RadioButton4.Text = "Brown"
        If RadioButton1.Checked = True Then
            Label3.Text = "2/10"
            Label4.Text = "Nice! That was Correct!
Great job!"
            Label4.ForeColor = Color.LimeGreen

        End If
        If Label3.Text = "2/10" Then
            Button4.Visible = True
        End If
        If RadioButton3.Checked = True Or RadioButton2.Checked = True Or RadioButton4.Checked = True Then
            Label4.ForeColor = Color.DarkRed
            Label4.Text = "Oh no! that was incorrect!
Try Again!"
        End If
        Button2.Text = "check"
        Button4.Text = "next"
        If Label3.Text = "2/10" Then
            Button3.Text = "next"
            Button4.Text = "Check"
        End If
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Button3.Visible = False
        Label1.Text = "What is Luke Lopez's shoe size?"
        RadioButton1.Checked = False
        RadioButton1.Text = "7"
        RadioButton2.Text = "8.5"
        RadioButton3.Text = "7.5"
        RadioButton4.Text = "8"
        Button3.Visible = False
        If RadioButton2.Checked = True Then
            Label3.Text = "3/10"
            Label4.Text = "Nice! That was Correct!
Great job!"
            Label4.ForeColor = Color.LimeGreen

        End If
        If Label3.Text = "3/10" Then
            Button5.Visible = True
            Button4.Visible = False
            Button3.Visible = False
            Button2.Visible = False
            Button1.Visible = False
        End If
        If RadioButton1.Checked = True Or RadioButton4.Checked = True Or RadioButton3.Checked = True Then
            Label4.ForeColor = Color.DarkRed
            Label4.Text = "Oh no! that was incorrect!
Try Again!"
        End If
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

    End Sub

这是我的前三个问题,所有的按钮、标签和单选按钮都会检查和更改每次点击,但我需要提高效率,因为我不想在这个迷你项目上浪费大量时间和计算机内存.

【问题讨论】:

他对高效编码提出了一些建议。 1)给你的变量和控件提供有用和有意义的名字,它增加了可读性,使调试等更有效。 2) 除非你真的有充分的理由,否则追求效率往往效率低下。您可能会发现在这种情况下处理数组的所有开销都比您拥有的编码使用更多的资源。 3)在可能的情况下,使用Lists over Arrays,处理起来更简单 4) 当今的计算机拥有如此多的可用资源,但往往没有,任何延迟的原因都是坐在椅子上驱动它 最后一点,codereview.stackexchange.com 专门用于处理改进代码。你可能会在那里得到一些建议 将你的问题和答案存储在代码中并不是一个好的设计。数据(问题和答案)应该与代码分开。代码将操纵数据。您不必使用数据库。您可以将数据存储在您在记事本中编写的文本文件中。 检查是否有任何答案可以帮助您处理此问题,如果有帮助,请考虑 accepting it。如果没有,请在此处提供更多详细信息。 【参考方案1】:

您可以将问题、选项和答案保存到数据库中,然后将它们读取到 DataTable 中,每次要显示问题时,只需使用 DataTable 中的 DataRow。这意味着您无需编辑以下问题:

使用'Label.TextChanged' event 处理'Label3.Text':

Private Sub Label3_TextChanged(sender As Object, e As EventArgs) Handles Label3.TextChanged
    Select Case Label3.Text
        Case "1/10"
            Button2.Visible = True
        Case "2/10"
            Button4.Visible = True
            Button3.Text = "next"
            Button4.Text = "Check"
        Case "3/10"
            ...
        ...
    End Select
End Sub

使用“If ... Else ...”语句。例如:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    '...


    If RadioButton1.Checked = True Then
        Label3.Text = "2/10"
        Label4.Text = "Nice! That was Correct! Great job!"
        Label4.ForeColor = Color.LimeGreen
    Else
        Label4.ForeColor = Color.DarkRed
        Label4.Text = "Oh no! that was incorrect! Try Again!"
    End If

    'If RadioButton3.Checked = True Or RadioButton2.Checked = True Or RadioButton4.Checked = True Then
    '    Label4.ForeColor = Color.DarkRed
    '    Label4.Text = "Oh no! that was incorrect! Try Again!"
    'End If

    '...
End Sub

【讨论】:

【参考方案2】:

在 cmets 中,我谈到了将数据存储在文本文件中。这就是我的文本文件的样子。每条记录都在一行上,每个字段用竖线字符 | 分隔。我选择了一个管道,因为您的问题和答案的文本中不会包含任何该字符。

1|Largest planet?|Jupiter|Venus|Saturn|Mars|1
2|Smallest planet?|Earth|Uranus|Mercury|Neptune|3
3|Comes closest to Earth?|Mars|Venus|Mercury|Jupiter|2
4|What planet is called the red planet?|Venus|Mars|Saturn|Neptune|2
5|What planet is furthest from Earth?|Saturn|Jupiter|Uranus|Neptune|4
6|How many planets in our solar system?|7|8|9|10|2
7|How many planets between Earth and Sun?|1|2|3|4|2
8|Which planet is said to have rings?|Jupiter|Uranus|Saturn|Neptune|3

您一直在使用类,因为 Form 是一个类。类可以具有属性、方法和事件等。现在上课,QuestionAnswer。属性非常简单。 Sub New 创建类的新实例,并使用参数设置类的所有属性。

Form.Load FillQuestionList 中被调用。此方法使用System.IO 中的File 类(为此需要添加Imports)。 ReadAllLines 返回文件中所有行的数组。我们可以遍历这些行,然后将每一行拆分为字段。 "|" 后面的小 c 告诉编译器你打算使用 Char 而不是 String。现在我们可以调用QuestionAnswerSub New,提供该行各部分的所有属性。然后将这个新实例添加到QuestionList,以便我们可以在程序中使用它。

接下来,Load 事件调用DisplayQuestion。此方法也将从btnNext click 事件中调用。我们从表单级变量CurrentQuestionIndex 中获取我们想要显示的索引。看看这个类是多么方便设置我们需要的值。循环遍历所有单选按钮,清除最后一个问题中的 Tag 并清除检查。该文件提供了正确答案的编号。这存储在单选按钮的 Tag 属性中。

当用户单击btnAnswer 时,我们会得到选择了哪个单选按钮并检查Tag 属性以查看答案是否正确。如果是,增加Score 并显示分数。

btnNext.Click 只是在增加索引并显示下一个问题之前检查我们是否已到达列表末尾。

Public Class QuestionGame

    Private QuestionList As New List(Of QuestionAnswer)
    Private CurrentQuestionIndex As Integer
    Private Score As Integer

    Private Sub QuestionGame_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        FillQuestionList()
        DisplayQuestion()
    End Sub

    Private Sub DisplayQuestion()
        Dim QA = QuestionList(CurrentQuestionIndex)
        lblQuestion.Text = QA.Question
        RadioButton1.Text = QA.Answer1
        RadioButton2.Text = QA.Answer2
        RadioButton3.Text = QA.Answer3
        RadioButton4.Text = QA.Answer4
        For Each rb In Controls.OfType(Of RadioButton)
            rb.Tag = ""
            rb.Checked = False
        Next
        Select Case QA.Correct
            Case 1
                RadioButton1.Tag = "Correct"
            Case 2
                RadioButton2.Tag = "Correct"
            Case 3
                RadioButton3.Tag = "Correct"
            Case 4
                RadioButton4.Tag = "Correct"
        End Select
    End Sub

    Private Sub FillQuestionList()
        Dim lines = File.ReadAllLines("C:\Users\xxxxxxxx\Questions.txt")
        For Each line In lines
            Dim parts = line.Split("|"c)
            QuestionList.Add(New QuestionAnswer(CInt(parts(0)), parts(1), parts(2), parts(3), parts(4), parts(5), CInt(parts(6))))
        Next
    End Sub

    Private Sub btnAnswer_Click(sender As Object, e As EventArgs) Handles btnAnswer.Click
        Dim rb = Controls.OfType(Of RadioButton)().FirstOrDefault(Function(r) r.Checked = True)
        If rb IsNot Nothing AndAlso rb.Tag.ToString = "Correct" Then
            Score += 1
            lblScore.Text = Score.ToString
            MessageBox.Show("Correct!")
            btnAnswer.Enabled = False 'The user can't increase his score by clicking answer several times
        Else
            MessageBox.Show("Sorry, wrong answer")
        End If
        btnNext.Enabled = True
    End Sub

    Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
        btnNext.Enabled = False 'user can't move to next question until he answers current question
        If CurrentQuestionIndex = QuestionList.Count - 1 Then
            MessageBox.Show("This is the last question")
        Else
            CurrentQuestionIndex += 1
            DisplayQuestion()
        End If
    End Sub
End Class

Public Class QuestionAnswer
    Public Property QuestionNumber As Integer
    Public Property Question As String
    Public Property Answer1 As String
    Public Property Answer2 As String
    Public Property Answer3 As String
    Public Property Answer4 As String
    Public Property Correct As Integer
    Public Sub New(Num As Integer, Ques As String, Ans1 As String, Ans2 As String, Ans3 As String, Ans4 As String, Cor As Integer)
        QuestionNumber = Num
        Question = Ques
        Answer1 = Ans1
        Answer2 = Ans2
        Answer3 = Ans3
        Answer4 = Ans4
        Correct = Cor
    End Sub
End Class

【讨论】:

以上是关于使用 Visual Studio 进行测验的主要内容,如果未能解决你的问题,请参考以下文章

用visual studio 2017来调试python

Visual Studio 2017 Enterprise 发布 15.3.2 版,附离线安装包下载。

微软发布了Visual Studio 2022 RC版,并将在11月8日发布正式版

微软发布了Visual Studio 2022 RC版,并将在11月8日发布正式版

visual studio 使用Resharper进行测试

使用 Visual Studio 进行 Scrapy 调试