在VB中编写一个简单的人口模型

时间:2015-08-30 11:53:30

标签: vb.net

我正在尝试在Visual Basics 2010中开发人口模型。但是,我不能做数学或代码!假设超过5代的苍蝇种群增长看起来像这样

GENERATION  JUVENILES  ADULTS  SENILES  TOTAL
0              10       10      10       30
1              20       10      10       40
2              20       20      10       50
3              40       20      20       80
4              40       40      20       100
5              80       40      40       160

在该模型中,Juvelines的存活率为1,即所有幼年都存活到成年期。成年人的存活率也是1,即所有成年人都存活到Seniles。然而,Seniles的存活率为0,即目前的人口都死亡。一旦完成,我将能够改变每种昆虫的存活率,但现在1,1,0工作正常 这里第二个最重要的假设是,青少年的出生率是目前成年人的两倍: 新的青少年数=当前成年人数*出生率 我的问题是如何将此模型表示为可视化基本代码?

这就是我到目前为止所做的一切

'Assign Values of Textbox to Public Variables
BirthRate = txtBirthRate.Text
GenerationNum = txtGenerations.Text
PopulationJuveniles = txtPJ.Text
PopulationAdults = txtPA.Text
PopulationSeniles = txtPS.Text
SurvivalJuveniles = txtSJ.Text
SurvivalAdults = txtSA.Text
SurvivalSeniles = txtSS.Text

For Counter As Integer = 1 To GenerationNum
    'Calculate population of adults
    NewJuveniles = PopulationAdults * BirthRate
    NewAdults = PopulationJuveniles * SurvivalJuveniles 'the juveniles have matured 
    NewSeniles = PopulationAdults * SurvivalAdults 'the adults have aged
Next

1 个答案:

答案 0 :(得分:1)

以下方法计算每一代的人口。请注意,它包含用于验证在TextBox中输入的数字的代码(否则错误的数字会导致异常)。它还显示ListBox中每一代的数字。

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
End Sub