我被要求制定一个程序来查找男性,女性和儿童的百分比,但我有点卡住 - 很难解释,但是当你进入一个年龄小于或等于17岁的年龄时,它会做一个孩子,任何事都可能是男性或女性。然而,当它被输入时,它被认为是两个人而不是一个,因为它要求年龄以后的性别,如果它不到17岁。如果很难解释,请告诉我,我会尝试改进我的方式告诉它。
当前代码:
Public Class Form1
Dim MalePercent, FemalePercent, ChildPercent As Single
Dim MaleCount, FemaleCount, ChildCount, CountTotal, Age As Integer
Dim Gender As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim buttonArray = {btnMale, btnFemale}
For Each button In buttonArray
button.Enabled = False
Next
MessageBox.Show("To start this program, please click a button of your choice.")
txtAge.Select()
txtAge.Clear()
End Sub
Private Sub btnMale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMale.Click
MaleCount += 1
CountTotal += 1
lblMaleCount.Text = MaleCount
lblCountTotal.Text = CountTotal
btnMale.Enabled = False
btnFemale.Enabled = False
txtAge.Enabled = True
txtAge.Select()
txtAge.Clear()
End Sub
Private Sub btnFemale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFemale.Click
FemaleCount += 1
CountTotal += 1
lblFemaleCount.Text = FemaleCount
lblCountTotal.Text = CountTotal
btnFemale.Enabled = False
btnMale.Enabled = False
txtAge.Enabled = True
txtAge.Select()
txtAge.Clear()
End Sub
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
MalePercent = MaleCount / CountTotal
lblMalePercent.Text = Format(MalePercent, "#0.00%")
FemalePercent = FemaleCount / CountTotal
lblFemalePercent.Text = Format(FemalePercent, "#0.00%")
ChildPercent = ChildCount / CountTotal
lblChildPercent.Text = Format(ChildPercent, "#0.00%")
End Sub
Private Sub btnFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinish.Click
MessageBox.Show("Thank-you for using this program. Good-bye!")
Close()
End Sub
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Age = txtAge.Text
If Age <= 17 Then
MessageBox.Show("Now please select the appropriate gender.")
ChildCount += 1
CountTotal += 1
lblChildCount.Text = ChildCount
lblCountTotal.Text = CountTotal
ElseIf Age >= 18 Then
MessageBox.Show("Now please select the appropriate gender.")
End If
txtAge.Enabled = False
Dim buttonArray = {btnMale, btnFemale}
For Each button In buttonArray
button.Enabled = True
Next
End Sub
答案 0 :(得分:2)
看起来,当你是一个孩子时,你会增加CountTotal
两次,一旦输入了年龄,就会再次选择性别。
试试这个:
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Age = txtAge.Text
If Age <= 17 Then
ChildCount += 1
'CountTotal += 1 <= remove this
lblChildCount.Text = ChildCount
'lblCountTotal.Text = CountTotal <= remove this
End If
'messagebox here because you always ask for gender no matter the age
MessageBox.Show("Now please select the appropriate gender.")
txtAge.Enabled = False
Dim buttonArray = {btnMale, btnFemale}
For Each button In buttonArray
button.Enabled = True
Next
End Sub