Vb.net Can';我不明白为什么我的变量不';不变

Vb.net Can';我不明白为什么我的变量不';不变,vb.net,Vb.net,我正在用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)

我正在用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
Dim总计为双精度=0.0
作为整数的Dim计时器=0
调暗时间为整数=0
按双倍计酬=0.0
而timeList.Items.Count<5
时间=时间列表。项目(计时器)
总计+=时间
计时器+=1
结束时

如果total>=0,total=0,total=21,total=31,total您的语法应该是这样的:

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

如果total>=0,total=0,total=21,total=31,total这将是我对控制台应用程序的修复

for进程将返回$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
模块1
副标题()
将时间列表变暗为新列表(整数)
AddRange(新整数(){1,2,3,4,5,6})
过程(时间列表)
timeList.Clear()
AddRange(新整数(){1,2,3,4})
过程(时间列表)
Console.Read()
端接头
私有子流程(时间列表为列表(整数))
总尺寸为双精度=0.0
作为整数的Dim计时器=0
调暗时间为整数=0
按双倍计酬=0.0
当timeList.Count<5且计时器如果total>=0,total=0,total=21,total=31,total,则可以使用函数方法更好地解决这一问题。要获取整数列表的总和,请执行以下操作:

Dim totalTime = timeList.Sum()

然后你就可以按照你提出的逻辑来做了。我强烈建议您学习使用Linq集合函数,使您的代码更可读、更容易理解。祝你好运。

从我在这里看到的情况来看,你的while循环不能被输入。正在呈现total=0的值。然后,所有乘法函数都将乘以0,导致结果为0。请尝试将total设置为1。我敢打赌答案是10。什么是时间主义?当你运行它时,其中有多少项?你还需要做一些事情来确保增加项。循环计数,否则它将是一个无限循环,你的计数必须超过5。我不确定你想做什么-但使用while循环是不正确的。尝试寻找循环。我总是假设不做任何假设,也许他真的只想处理列表中的前4项?您的解决方案将处理所有问题them@SkyVar,您应该在项目属性中启用“严格打开”和“显式打开”,列表框包含字符串,并且您正在将它们添加到一个数字中,这将被这些属性捕获setting@SkyVar,对于intCount=0到timeList.Items.Count-1将修复此问题issue@SkyVar^^这是正确的。“在项目属性中启用严格打开和显式打开”将是最佳修复方法。否则,“For intCount=0 To timeList.Items.Count-1”将作为解决方案。弗雷多-+1秒@SkyVar,项目的属性,而不是listbox。右键单击解决方案资源管理器中的项目文件,转到左侧的“编译”选项卡