卡路里倍增器

时间:2015-01-09 16:16:38

标签: vb.net

我正在尝试编制一个计算器,计算出你根据平均速度燃烧了多少卡路里。目前我无法将卡路里烧得正确。

Dim Distance As Double
    Dim Duration As Double
    Dim Speed As Long
    Dim Caloriesburnt As Integer
    Dim Multiplier As Integer

    Distance = nupDistance.Value
    Duration = nupDuration.Value

    Speed = Distance / Duration
    txtAverageSpeed.Text = Speed


    If txtAverageSpeed.Text <= 5 Then
        Multiplier = 472

    ElseIf 5 < txtAverageSpeed.Text <= 6 Then
        Multiplier = 590

    ElseIf 6 < txtAverageSpeed.Text <= 7 Then
        Multiplier = 679

    ElseIf 7 < txtAverageSpeed.Text <= 8 Then
        Multiplier = 797

    ElseIf 8 < txtAverageSpeed.text <= 9 Then
        Multiplier = 885

    Else
        Multiplier = 994

    End If
    Caloriesburnt = Multiplier * Duration
    txtCaloriesBurnt.Text = Caloriesburnt

2 个答案:

答案 0 :(得分:2)

你已经有了变速,不要使用文本框。通过严格选项,您会发现错误。将字符串与数字进行比较并不是一个好主意。

您应该阅读如何在VB中正确使用if statment。做5&lt; x <= 6不会达到预期效果,你必须做x&lt; 5和x <= 6.通过使用else,您可以省略第一次检查。

If Speed <= 5 Then
    Multiplier = 472
ElseIf Speed <= 6 Then
    Multiplier = 590
ElseIf Speed <= 7 Then
    Multiplier = 679
ElseIf Speed <= 8 Then
    Multiplier = 797
ElseIf Speed <= 9 Then
    Multiplier = 885
Else
    Multiplier = 994
End If

答案 1 :(得分:0)

你的速度&#34;变量的类型为long,这意味着它的积分。你怎么能有一个&gt;的价值5和&lt;那么6?它只是6!

这也可以使用Select Case声明来完成:

Select Case speed
    Case Is <= 5
        Multiplier = 472
    Case 6
        Multiplier = 590
    Case 7
        Multiplier = 679
    Case 8
        Multiplier = 797
    Case 9
        Multiplier = 885
    Case Else
        Multiplier = 994
End Select