我希望代码输出从1年到5年的每个折旧年,目前它对所有5个值只显示1个

时间:2019-05-07 17:43:34

标签: vb.net visual-studio

下面的代码每年显示“ 1”,并且应为“ 1”至“ 5”

Public Class Form1
    Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

        Dim cost As Double
        Dim life As Double = CDbl(ListBox1.SelectedItem)
        Dim salvage As Double
        Dim numberperiod As Integer
        Dim period As Integer
        Dim depreciation1 As Double
        Dim depreciation2 As Double
        Dim isconverted1 As Boolean
        Dim isconverted2 As Boolean
        Dim isconverted3 As Boolean
        Dim year As Integer = 0

        isconverted1 = Double.TryParse(textbox1.Text, cost)
        isconverted2 = Double.TryParse(textbox2.Text, salvage)
        isconverted3 = Integer.TryParse(ListBox1.SelectedItem, period)

        lstDCB.Items.Add("Year        Depreciation")
        lstSOTY.Items.Add("Year        Depreciation")

        year = Val(year) + 1

        For numberperiod = 1 To period Step 1
            depreciation1 = Financial.DDB(cost, salvage, life, numberperiod)
            depreciation2 = Financial.SYD(cost, salvage, life, numberperiod)

            lstDCB.Items.Add(year & "              " & Math.Round(depreciation1, 2, MidpointRounding.AwayFromZero))
            lstSOTY.Items.Add(year & "              " & Math.Round(depreciation2, 2, MidpointRounding.AwayFromZero))

        Next numberperiod

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Close()
    End Sub
End Class

它应该在DDB和SYD的年份下显示1到5。

1 个答案:

答案 0 :(得分:0)

如果您查看发布的代码,则在将变量year初始化为零,然后使用语句year = Val(year) + 1更新之后,再也不会在例程中更新变量Module VBModule Sub Main() Dim cost As Double = 100 Dim life As Double = 12 Dim salvage As Double = 3 Dim numberperiod As Integer Dim period As Integer = 3 Dim depreciation1 As Double Dim depreciation2 As Double Dim year As Integer = 0 For numberperiod = 1 To period Step 1 depreciation1 = Financial.DDB(cost, salvage, life, numberperiod) depreciation2 = Financial.SYD(cost, salvage, life, numberperiod) Console.WriteLine(year & " " & Math.Round(depreciation1, 2, MidpointRounding.AwayFromZero)) Console.WriteLine(year & " " & Math.Round(depreciation2, 2, MidpointRounding.AwayFromZero)) year += 1 Next numberperiod End Sub End Module

我相信您打算将该语句放入循环体内(在计算之后),以便在循环的每次迭代中递增。

类似这样的东西:

{{1}}