Visual Studio VB中的简单测验

toiithl6  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(66)

我正在上一门和我的专业完全不相关的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
unhi4e5o

unhi4e5o1#

有三个问题,所以肯定有三个如果......如果......如果......或者如果......你不应该在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

相关问题