我正在上一门和我的专业完全不相关的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条答案
按热度按时间unhi4e5o1#
有三个问题,所以肯定有三个如果......如果......如果......或者如果......你不应该在ElseIf中包含其他问题的评分,因为一旦程序在你的条件中的If或ElseIf中返回true,If..Else就会终止,并且不执行ElseIf的其余部分。
比如说应该是这样,
请放心,它会按照您的要求执行......或者像这样……