-1

我正在编写一个简单的 VB 表格来计算电费的成本。源代码如下:

Private Sub BtnCalculate_Click(sender As Object, e As EventArgs) Handles BtnCalculate.Click

        Dim LowUnits As Integer
        'LowUnits is the cost per unit if the integer is lower than [Difference Variable]
        Dim HighUnits As Integer
        'HighUnits is the cost per unit if the integer is higher than [Difference Variable]
        Dim FixedFee As Integer
        'This is the fixed infrastructure fee that all providers charge alongside unit cost
        Dim Difference As Integer
        'Number of units before a premium price [High Units Variable] is charged
        Dim UnitNo As Integer
        'Number of Units that is inputted
        Dim Price As Double


        UnitNo = UnitBox.Text

        If RdoEE.Checked = True Then
            LowUnits = 0.04
            HighUnits = 0.06
            FixedFee = 10
            Difference = 500

        ElseIf RdoPG.Checked = True Then
            LowUnits = 0.04
            HighUnits = 0.06
            FixedFee = 15
            Difference = 600

        ElseIf RdoBG.Checked = True Then
            LowUnits = 0.03
            HighUnits = 0.05
            FixedFee = 20
            Difference = 500

        End If

        If UnitNo <= Difference Then
            Price = 1
            Price = Price * UnitNo
            Price = UnitNo * LowUnits
            Price = Price + FixedFee
        Else
            'Price = ((((Price + 1) * UnitNo) * HighUnits) + FixedFee)

        End If


        MsgBox("Your cost is £" & Price & "")



    End Sub
End Class

该程序是这样工作的:您输入编号。每月的电力单位,选择一个供应商(具有不同的费率以及固定的基础设施费用),这会输出总成本。我遇到的问题是程序似乎没有检测到单位的成本并将它们存储在价格变量中,并且只包括固定费用并且不计算单位。到目前为止,我还没有完成错误处理,因为我的重点是让程序正常工作,这对我来说是一个优先事项。

我必须满足的标准是:“编写一个程序,计算供应商的单位成本并为客户输出总成本。程序必须选择 3 个不同的供应商(EDF Energy、PowerGen 和 British Gas),并且必须输出总成本。EDF 能源收费 0.04 英镑/单位,最高 500 单位,然后 0.06 英镑/单位,超过 500 单位,包括 10 英镑的固定基础设施费用。PowerGen 收费 0.04 英镑/单位,最高 600 单位,然后 0.06 英镑/单位超过 600 个单位,包括 15 英镑的固定基础设施费用。British Gas 收取 0.03 英镑/单位,然后在 500 个单位之后收取 0.05 英镑/单位,包括 20 英镑的固定基础设施费用。

4

2 回答 2

2

您在值中存储小数值(例如0.04Integer,这是您的问题,而 VB.NET 不会警告您精度损失。

将本地变量从更改IntegerDecimal- 当使用货币值时总是更喜欢Decimal,因为无法准确表示Double 简单的值0.1。或者更好:使用整数,但使用便士而不是英镑作为基值(所以1== £0.01 和100== £1)。

其他一些提示:

  • 本地值应使用camelCase大小写,而不是TitleCase.
  • 不要使用匈牙利符号,它现在被认为是不好的做法(并且在许多代码库中被禁止)。它是现代编译器和编辑器之前的时代遗物。但是,仍然可以接受带有 UI 组件指示的名称后缀(因此请考虑britishGasRadioButton用 代替RdoBG)。
  • Option Strict在 VB.NET 中启用。如果启用此选项,您将收到有关缩小操作的编译器警告和错误,这是您在尝试将值缩小DoubleInt32值时遇到的情况。
  • 您不需要检查= True已经是布尔值的值,因此If RdoEE.Checked = True可以简化为If edfEnergyRadio.Checked Then
于 2015-10-09T18:59:08.273 回答
1

I recommend that you use Option Strict On.

That would highlight a problem with your code:

Dim lowUnits As Integer
'...
lowUnits = 0.04

Can you see that you are trying to assign a floating-point value to a variable of Integer type? Visual Studio would have put a wavy red line under 0.04 and given you a message that "Option Strict On disallows implicit conversions from 'Double' to 'Integer'."

However, when dealing with numbers as currency, it is usually a Good Idea to use the Decimal data type because it does not have the same rounding errors as representing exact decimal numbers as a Double has.

There is a fairly comprehensive guide regarding using floating-point numbers in computer programming at What Every Computer Scientist Should Know About Floating-Point Arithmetic.

So, the answer is to declare and use:

Dim lowUnits As Decimal
'...
lowUnits = 0.04D

where the D suffix tells the compiler that 0.04 is a Decimal.

于 2015-10-09T19:04:43.073 回答