1

我正在用 VB 构建一点时间 > 付费转换程序。我真的是 VB 的新手,不明白为什么我的浮动工资没有按应有的方式计算。我插入 5 个 5 作为测试并得到 0 美元。

Dim total As Double = 0.0
Dim timeCounter As Integer = 0
Dim time As Integer = 0
Dim pay As Double = 0.0

While timeList.Items.Count < 5
    time = timeList.Items(timeCounter)
    total += time
    timeCounter += 1
End While

If total >= 0 And total <= 40 Then
    If total >= 0 And total <= 20 Then
        pay = total * 10
    ElseIf total >= 21 And total <= 30 Then
        pay = total * 12
    ElseIf total >= 31 And total <= 40 Then
        pay = total * 15
    Else
        PayLabel.Text = "Error"
    End If
End If

PayLabel.Text = "$" & pay
4

3 回答 3

2

你的语法应该是这样的:

For intCount = 0 To timeList.Items.Count
    time = timeList.Items(intCount)
    total += time
Next intCount

这将避免无限循环。

要解决您的 40+ 问题:

If total >= 0 And total <= 40 Then
    If total >= 0 And total <= 20 Then
        pay = total * 10
    ElseIf total >= 21 And total <= 30 Then
        pay = total * 12
    ElseIf total >= 31 And total <= 40 Then
        pay = total * 15
    End If
Else
    PayLabel.Text = "Error"            
End If
于 2013-09-12T23:01:44.650 回答
1

这将是我对控制台应用程序的修复

对于流程将返回 $0,第二个 $100

Module Module1

Sub Main()
    Dim timeList As New List(Of Integer)

    timeList.AddRange(New Integer() {1, 2, 3, 4, 5, 6})
    process(timeList)
    timeList.Clear()
    timeList.AddRange(New Integer() {1, 2, 3, 4})
    process(timeList)

    Console.Read()


End Sub

Private Sub process(timeList As List(Of Integer))
    Dim total As Double = 0.0
    Dim timeCounter As Integer = 0
    Dim time As Integer = 0
    Dim pay As Double = 0.0
    While timeList.Count < 5 AndAlso timeCounter < timeList.Count
        time = timeList(timeCounter)
        total += time
        timeCounter += 1
    End While

    If total >= 0 And total <= 40 Then
        If total >= 0 And total <= 20 Then
            pay = total * 10
        ElseIf total >= 21 And total <= 30 Then
            pay = total * 12
        ElseIf total >= 31 And total <= 40 Then
            pay = total * 15
        Else
            Console.WriteLine("error")
        End If
    End If

    Console.WriteLine("$" & pay)
End Sub

End Module
于 2013-09-12T23:02:22.787 回答
0

这可以通过函数式方法更好地解决。要获得整数列表的总和,请执行以下操作:

Dim totalTime = timeList.Sum()

然后,您可以按照您制定的逻辑进行操作。我强烈建议学习使用 Linq Set Functions 来使您的代码更易读和更容易理解。祝你好运。

于 2013-09-12T23:51:13.737 回答