在Visual Basic中查找总付款

时间:2016-06-04 20:41:01

标签: vb.net payment

我正在尝试创建一个程序,可以找到用户在进入工作时间后可以获得的付款,(总共5个条目,最多不超过40小时)。有人可以向我解释一下吗?我是视觉基础的新手,我不知道为什么我没有得到输出。

' Counter-controlled repetition: Time Entry problem.
Public Class TimeEntry

    Private Sub SubmitTimeButton_Click(sender As Object, e As EventArgs) Handles SubmitTimeButton.Click

        'if user entered hours
        If Enterhourstextbox.Text <= 5 Then
            ' add the hours to the TimeRecordsListBox
            TimeRecordsListBox.Items.Add(Enterhourstextbox.Text)
            Enterhourstextbox.Clear() 'clear the Enterhourstextbox
            Enterhourstextbox.Focus() ' select the Enterhourstetbox
        End If

        ' determine whether to prevent the user from entering more results
        If TimeRecordsListBox.Items.Count = 5 Then
            SubmitTimeButton.Enabled = False ' disables SubmitTimeButton
            Enterhourstextbox.Enabled = False ' disables Enterhourstextbox
            GetpaymentButton.Enabled = True ' enable GetpaymentButton
        End If

    End Sub ' submitTimeButton_Click

    ' calculates the total hours based on the hours in TimeRecordsListBox
    Private Sub GetpaymentButton_Click(sender As Object, e As EventArgs) Handles GetpaymentButton.Click

        Dim hourtotal As Integer ' sum of hours entered by user
        Dim hourCounter As Integer ' counter for hours 
        Dim hour As Integer ' hour input by user
        Dim total As Integer ' payment total

        ' initialization phase
        hourtotal = 0 ' set total to zero before adding hours to it
        hourCounter = 0 ' prepare to loop

        ' processing phase
        Do While hourCounter < TimeRecordsListBox.Items.Count
            hour = TimeRecordsListBox.Items.Add(hourCounter) ' get next hour
            hourtotal += hour
            hourCounter += 1 ' add 1 to hourCounter
        Loop

        ' termination phase
        If hourCounter <= 0 Or hourCounter >= 15 Then
            getpaymentlabel.Text = hourtotal * 9
        End If

        If hourCounter <= 16 Or hourCounter >= 25 Then
            getpaymentlabel.Text = hourtotal * 13
        End If

        If hourCounter <= 26 Or hourCounter >= 40 Then
            getpaymentlabel.Text = hourtotal * 20
        End If

    End Sub ' calculateAverageButton_Click

    Private Sub CleartimeButton_Click(sender As Object, e As EventArgs) Handles CleartimeButton.Click

        TimeRecordsListBox.Items.Clear() ' removes all items
        getpaymentlabel.Text = String.Empty ' enables submitResultButton
        Enterhourstextbox.Clear() ' remoces items from EnterhoursTextBox

    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

您的 GetpaymentButton_Click 方法确实存在问题。 我们来看看它。

首先,你有一堆变量声明

  • hourtotal
  • hourCounter
  • 小时

<强> hourtotal

您在sub的开头声明此整数,但您从不为其指定任何值。您只能在终止阶段中使用它。

    ' termination phase
If hourtotal <= 0 Or hourtotal >= 15 Then
    total = hourtotal * 9
    getpaymentlabel.Text = hourtotal * 9

由于您从未分配任何内容,因此它的值始终为 0

这是你的问题。你可以替换

getpaymentlabel.Text = hourtotal * 9

getpaymentlabel.Text = 0 * 9

并且它与您不做任何更改变量值的操作相同。

总计小时

这些变量已分配但从未使用过。然后,你可以完全删除它们。

这样的事情(见下文)更有可能奏效。 我重新引擎了一点,但重要的是现在使用了hourtotal。

Private Sub GetpaymentButton_Click(sender As Object, e As EventArgs)
    Dim Hourtotal As Integer ' sum of hours entered by user

    ' processing phase

    For Each TimeentryValue As object In TimeRecordsListBox.Items
        Dim CurrentEntry As Integer

        If Not Integer.TryParse(TimeentryValue, CurrentEntry) Then
            Hourtotal = -1 '  If one of the entry was not an integer :( 
            Exit For
        End If

        Hourtotal += CurrentEntry
    Next

   ' termination phase

    If Hourtotal <= 0 Or Hourtotal >= 15 Then
        getpaymentlabel.Text = Hourtotal * 9
    End If
End Sub ' calculateAverageButton_Click

列表框项目是“对象”,所以除了所有这些之外,我还使用了

Integer.TryParse

确保列表框项在将其添加到hourtotal之前实际上是一个整数。如果没有,那么你的应用程序不会崩溃,因为你试图添加一些不可能转换成小时总计的整数。

你可能需要调整我给你的东西或者根本不使用它。 要纠正你的问题,你所要做的就是在某个时刻实际增加总数,这是你目前没做的事情,而且你的标签的原因总是 0