VB中的简单测验

时间:2015-03-17 01:05:46

标签: vb.net visual-studio

我正在参加这个与我的专业完全无关的vb普通教育课程。我真的在这里与我们的任务之一挣扎。作业是创建一个由3个问题组成的简单测验,该程序应该计算得分和参与者的百分比。到目前为止,我已经编写了以下代码,但它没有正确执行。有人可以帮我这个吗?我无法弄清楚的另一件事是如何让其中一个文本框不区分大小写。任何帮助都会很棒!

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    Dim FirstAnswer As Integer = 2
    Dim SecondAnswer As String = "Barak Obama"
    Dim ThirdAnswer As String = "Florida"
    Dim CorrectAnswer As Double = 0
    Dim Num1 As Double = CDbl(lblCorrectAnswer.Text)
    Dim Percent As Double
    Percent = Num1 / 3 * 100

    If txtSum.Text = FirstAnswer And txtPresident.Text = SecondAnswer And txtImpoertantQues.Text = ThirdAnswer Then
        lblCorrectAnswer.Text = CorrectAnswer + 3
        txtSum.BackColor = Color.Green
        txtSum.ForeColor = Color.White
        txtPresident.BackColor = Color.Green
        txtPresident.ForeColor = Color.White
        txtImpoertantQues.BackColor = Color.Green
        txtImpoertantQues.ForeColor = Color.White
        lblPrecent.Text = Percent.ToString("##.00") & "%"
    ElseIf txtSum.Text = FirstAnswer And txtImpoertantQues.Text = ThirdAnswer Then
        lblCorrectAnswer.Text = CorrectAnswer + 2
        txtSum.BackColor = Color.Green
        txtSum.ForeColor = Color.White
        txtImpoertantQues.BackColor = Color.Green
        txtImpoertantQues.ForeColor = Color.White
        txtPresident.BackColor = Color.Red
        txtPresident.ForeColor = Color.White
        lblPrecent.Text = Percent.ToString("##.00") & "%"
    ElseIf txtSum.Text = FirstAnswer Then
        lblCorrectAnswer.Text = CorrectAnswer + 1
        txtSum.BackColor = Color.Green
        txtSum.ForeColor = Color.White
        txtPresident.BackColor = Color.Red
        txtPresident.ForeColor = Color.White
        txtImpoertantQues.BackColor = Color.Red
        txtImpoertantQues.ForeColor = Color.White
        lblPrecent.Text = Percent.ToString("##.00") & "%"
    ElseIf txtSum.Text = FirstAnswer And txtPresident.Text = SecondAnswer Then
        lblCorrectAnswer.Text = CorrectAnswer + 2
        txtSum.BackColor = Color.Green
        txtSum.ForeColor = Color.White
        txtPresident.BackColor = Color.Green
        txtPresident.ForeColor = Color.White
        txtImpoertantQues.BackColor = Color.Red
        txtImpoertantQues.ForeColor = Color.White
        lblPrecent.Text = Percent.ToString("##.00") & "%"
    ElseIf txtPresident.Text = SecondAnswer And txtImpoertantQues.Text = ThirdAnswer Then
        lblCorrectAnswer.Text = CorrectAnswer + 2
        txtPresident.BackColor = Color.Green
        txtPresident.ForeColor = Color.White
        txtImpoertantQues.BackColor = Color.Green
        txtImpoertantQues.ForeColor = Color.White
        txtSum.BackColor = Color.Red
        txtSum.ForeColor = Color.White
        lblPrecent.Text = Percent.ToString("##.00") & "%"
    ElseIf txtPresident.Text = SecondAnswer Then
        lblCorrectAnswer.Text = CorrectAnswer + 1
        txtPresident.BackColor = Color.Green
        txtPresident.ForeColor = Color.White
        txtSum.BackColor = Color.Red
        txtSum.ForeColor = Color.White
        txtImpoertantQues.BackColor = Color.Red
        txtImpoertantQues.ForeColor = Color.White
        lblPrecent.Text = Percent.ToString("##.00") & "%"
    ElseIf txtImpoertantQues.Text = ThirdAnswer Then
        lblCorrectAnswer.Text = CorrectAnswer + 1
        txtImpoertantQues.BackColor = Color.Green
        txtImpoertantQues.ForeColor = Color.White
        txtSum.BackColor = Color.Red
        txtSum.ForeColor = Color.White
        txtPresident.BackColor = Color.Red
        txtPresident.ForeColor = Color.White
        lblPrecent.Text = Percent.ToString("##.00") & "%"
    Else
        lblCorrectAnswer.Text = CorrectAnswer = 0
        lblPrecent .Text = 0
        txtSum.BackColor = Color.Red
        txtSum.ForeColor = Color.White
        txtPresident.BackColor = Color.Red
        txtPresident.ForeColor = Color.White
        txtImpoertantQues.BackColor = Color.Red
        txtImpoertantQues.ForeColor = Color.White
    End If

    If txtSum.Text = Nothing Or txtPresident.Text = Nothing Or txtPresident.Text = Nothing Then
        MessageBox.Show("ERROR! Please enter an aswer.")
    End If

1 个答案:

答案 0 :(得分:1)

有三个问题所以必须有三个If ... ElseIf ... Else或If..Else。 你不应该在ElseIf中包含其他问题的评分,因为一旦程序在你的条件中的If或ElseIf中返回true,那么If..Else终止而不执行其余的ElseIf ..

它应该是这样的,例如,

if txtsum.text=.... then
elseif
else
end if

if txtpresident.text=... then
else
end if

if txtImpoertantQues.Text=... then
else
end if

请放心,它会按照您的意愿执行...... 或者可能是这样......

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    Dim FirstAnswer As Integer = 2
    Dim SecondAnswer As String = "Barak Obama"
    Dim ThirdAnswer As String = "Florida"
    Dim CorrectAnswer As Double = 0
    Dim Num1 As Double = CDbl(lblCorrectAnswer.Text)
    Dim Percent As Double
    Percent = Num1 / 3 * 100
    If txtSum.Text = Nothing Or txtPresident.Text = Nothing Or txtPresident.Text = Nothing Then
        MessageBox.Show("ERROR! Please enter an aswer.")
        'when everything is empty then the rest won't be executed
        'instead, the program prompts the user to enter an answer
    Else
        If txtSum.Text = FirstAnswer Then
            CorrectAnswer = CorrectAnswer + 1 'added points
            txtSum.BackColor = Color.Green
            txtSum.ForeColor = Color.White
            lblPrecent.Text = Percent.ToString("##.00") & "%"
        End If

        If txtSum.Text = FirstAnswer Then
            CorrectAnswer = CorrectAnswer + 1 'added points
            txtSum.BackColor = Color.Green
            txtSum.ForeColor = Color.White
            lblPrecent.Text = Percent.ToString("##.00") & "%"
        End If

        If txtImpoertantQues.Text = ThirdAnswer Then
            CorrectAnswer = CorrectAnswer + 1 'added points
            txtImpoertantQues.BackColor = Color.Green
            txtImpoertantQues.ForeColor = Color.White
            lblPrecent.Text = Percent.ToString("##.00") & "%"
        End If

        lblCorrectAnswer.Text = CorrectAnswer 'displays the total score

    End If 'end of empty field validation
End Sub