VB.net结合while循环

时间:2014-04-20 22:23:38

标签: vb.net loops while-loop

过去两个小时我一直在研究这个项目,我完全被难过了。简而言之,用户将在第一个文本框中输入总体,在第二个文本框中输入增长率。然后程序计算未来75年的人口和人口差异,并将其显示在第三个文本框中。以下是该程序的截图:
enter image description here

这是我的代码到目前为止的样子:

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim num As Double = System.Convert.ToDouble(TextBox1.Text)
    Dim num1 As Double = System.Convert.ToDouble(TextBox2.Text)
    Dim num2 As Double = num1 / (100 + 1)
    TextBox3.Text = "Year      Population    Increase"
    Dim num3 As Integer = 1

    While num3 <= 75
        Dim num4 As Double = num
        num = num * num2
        Dim num5 As Double = num - num4
        Dim text As String = TextBox3.Text, "" & vbCrLf & "", num3.ToString(), "            ", num.ToString("F"), "      ", num5.ToString("F")
        TextBox3.Text = System.Convert.ToString(text)
        num3 = num3 + 1

    End While

End Sub

我似乎无法想象如何将“年人口增长”与我的while循环中的计算结合起来。如果以某种方式可以指出我如何能够实现这一目标的正确方向,那将非常感激。

提前致谢!

3 个答案:

答案 0 :(得分:3)

更简洁的版本:

Dim popBasis As Double = Convert.ToDouble(TextBox1.Text)
Dim growthRate As Double = Convert.ToDouble(TextBox2.Text)

growthRate = growthRate / 100
' cannot anchor "rows" in a ML TB, so make this some labels:
'TextBox3.Text = "Year      Population    Increase"

Dim popIncr as Double

For n As Integer = 1 to 75
    popIncr = popBasis * growthRate
    popBasis += popIncr

    TextBox3.Text &= n.ToString & vbTab & popBasis.Tostring & 
         vbTab & popIncr.ToString & Environment.NewLine

    ' or:
    TextBox3.Text &= String.Format({0} {1} {2} {3}, n.ToString, 
             popBasis.Tostring, popIncr.ToString, Environment.NewLine)

Next n

使用String.Format方法,您可以修改使用PadLeft / PadRight来模拟列。这仅适用于等宽字体。另一种方法是使用具有实际列标题的ListView

答案 1 :(得分:1)

可能使用vbTab

Dim text As String = "Year" & vbTab & "Population" & vbTab & "Increase" & vbCrLf

While num3 <= 75
    Dim num4 As Double = num
    num = num * num2
    Dim num5 As Double = num - num4
    text = text & num3.ToString() & vbTab & num.ToString("F") & vbTab & num5.ToString("F") & vbCrLf
    num3 = num3 + 1
End While
TextBox3.Text = text

TextBox3是多行的

答案 2 :(得分:1)

第一个建议,以描述性的方式命名您的变量。 其次,在while循环之外声明变量文本。

Dim text as string = string.empty

第三个建议,在你的循环中做这样的事情,但如果你使用StringBuilder来连接字符串会更好。

While num3 <= 75
        Dim num4 As Double = num
        num = num * num2
        Dim num5 As Double = num - num4
         text  =  text & num3.ToString() & vbtab & num.ToString("F") & vbtab & num5.ToString("F") & vbcrlf

        num3 = num3 + 1

    End While

然后在循环外的textbox3中分配变量文本。

textbox3.text = textbox3.text + vbcrlf + text

总而言之,您的代码将类似于:

Dim num As Double = System.Convert.ToDouble(TextBox1.Text)
    Dim num1 As Double = System.Convert.ToDouble(TextBox2.Text)
    Dim num2 As Double = num1 / (100 + 1)
    TextBox3.Text = "Year      Population    Increase"
    Dim num3 As Integer = 1

Dim text as string = string.empty

While num3 <= 75
        Dim num4 As Double = num
        num = num * num2
        Dim num5 As Double = num - num4
          text  =  text & num3.ToString() & vbtab & num.ToString("F") & vbtab & num5.ToString("F") & vbcrlf

        num3 = num3 + 1

    End While

textbox3.text = textbox3.text + vbcrlf  + text