VB中的人口模型 - 不接受输入值

时间:2015-11-12 20:10:03

标签: vb.net integer double

这是我的代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Birthrate As Double, GenerationNum As Integer
        Dim PopulationJuveniles, PopulationAdults, PopulationSeniles As Double
        Dim SurvivalJuveniles, SurvivalAdults, SurvivalSeniles As Double
        Dim NewJuveniles, NewAdults, NewSeniles As Double

        If Not Double.TryParse(txtBirthRate.Text, Birthrate) _
          OrElse Integer.TryParse(txtGenerations.Text, GenerationNum) Then
            MessageBox.Show("Enter valid numbers for birthrate and generations")
            Exit Sub
        End If

        If Not Double.TryParse(txtPJ.Text, PopulationJuveniles) _
          OrElse Double.TryParse(txtPA.Text, PopulationAdults) _
          OrElse Double.TryParse(txtPS.Text, PopulationSeniles) Then
            MessageBox.Show("Enter valid numbers for populations")
            Exit Sub
        End If

        If Not Double.TryParse(txtSJ.Text, SurvivalJuveniles) _
          OrElse Double.TryParse(txtSA.Text, SurvivalAdults) _
          OrElse Double.TryParse(txtSS.Text, SurvivalSeniles) Then
            MessageBox.Show("Enter valid numbers for survival")
            Exit Sub
        End If

        ListBox1.Items.Clear()
        For Counter As Integer = 1 To GenerationNum
            NewJuveniles = PopulationAdults * Birthrate
            NewAdults = PopulationJuveniles * SurvivalJuveniles 'the juveniles have matured 
            NewSeniles = PopulationAdults * SurvivalAdults 'Add + PopulationSeniles * SurvivalSeniles if SurvivalSeniles can ever be >0
            PopulationSeniles = NewSeniles
            PopulationAdults = NewAdults
            PopulationJuveniles = NewJuveniles
            ListBox1.Items.Add(PopulationJuveniles.ToString & ", " & PopulationAdults.ToString & ", " & PopulationSeniles.ToString)
        Next

当我尝试为整数输入双倍数值10.1和5等值时,只需输入有效数字但它们是有效数字!

任何人都有任何建议或数字,我可以尝试看看它是否有效。

1 个答案:

答案 0 :(得分:1)

您需要在评估的每个语句中使用关键字NOT。

例如:

If Not Double.TryParse(txtPJ.Text, PopulationJuveniles) _
  OrElse Double.TryParse(txtPA.Text, PopulationAdults) _
  OrElse Double.TryParse(txtPS.Text, PopulationSeniles) Then
    MessageBox.Show("Enter valid numbers for populations")
    Exit Sub
End If

应改为:

If Not Double.TryParse(txtPJ.Text, PopulationJuveniles) _
  OrElse Not Double.TryParse(txtPA.Text, PopulationAdults) _
  OrElse Not Double.TryParse(txtPS.Text, PopulationSeniles) Then
    MessageBox.Show("Enter valid numbers for populations")
    Exit Sub
End If

说明:

在VB.Net中,每个布尔表达式都是自己计算的,所以第一个不仅影响第一个表达式。如果您希望每个表达式具有相同的功能,则每个表达式都需要自己的NOT关键字。