使用单选按钮和复选框进行计算

时间:2016-02-22 23:57:42

标签: vb.net

我是一名初学程序员,正试图让我的程序计算与无线电盒和复选框一起使用。

我需要跟踪总数,所以如果有人选择了一辆车,那么你选择的车就会有乘数。恩。宝马= BasePrice的1.5 = 20,000 ... = 30000.所以如果你添加了敞篷车,车轮性能等选项,它会增加乘数。

我知道它有点草率,在现实世界的应用程序中没有意义,因为汽车有这个和那个。它只是举例说明并帮助我进行计算。

如何选择常量并将其分配给变量,以便计算价格?一旦我开始切换汽车,我就无法乘以乘数值。因此,如果你在宝马的皮革中获得1.5以上的皮革,但如果你在雪佛兰获得皮革,那么只需乘以1.0。一旦我开始转换,我就无法获得超出其统一费率的选项。我有我的程序中不同汽车的代码,只是把它留下来增强可读性。 -

宝马与天窗和车轮升级将乘以1.5 雪佛兰与天窗和车轮升级将乘以1.0 当您选中并取消选中并切换选项时,Cost也会切换。

感谢任何帮助,谢谢你们。

Const BasePrice As Double = 20000
Public Const BMW As Double = 1.5
Public Const Chevrolet As Double = 1.0
Public Const Leather As Double = 2000
Public Const Sunroof As Double = 1000
Public Const GPS As Double = 2000
Public Const Performance As Double = 3000
Public Const Wheels As Double = 2000

Dim Sum As Double
Dim options As Double


Sub calculate()
    If optBMW.Checked Then
        Sum = BMW * (BasePrice + options)
        lblCost.Text = FormatCurrency(Sum)
    End If
End Sub

Private Sub optBMW_CheckedChanged(sender As Object, e As EventArgs) Handles optBMW.CheckedChanged
    calculate()
End Sub

Private Sub chkLeather_CheckedChanged(sender As Object, e As EventArgs) Handles chkLeather.CheckedChanged
    If chkLeather.Checked = True Then
        Sum += Leather
        lblCost.Text = Sum
    End If

    If chkLeather.Checked = False Then
        Sum -= Leather
        lblCost.Text = Sum
    End If


End Sub

Private Sub chkWheelUpgrade_CheckedChanged(sender As Object, e As EventArgs) Handles chkWheelUpgrade.CheckedChanged


End Sub

界面图片

enter image description here

http://i.stack.imgur.com/V9PJZ.png

2 个答案:

答案 0 :(得分:0)

试试这个:

   Public BasePrice As Double = 20000
   Public BMW As Double = 1.5
   Public Chevrolet As Double = 1.0
   Public Leather As Double = 2000
   Public Sunroof As Double = 1000
   Public GPS As Double = 2000
   Public Performance As Double = 3000
   Public Wheels As Double = 2000

   Dim Sum As Double = 0
   Dim options As Double = 0


   Sub calculate()
       If optBMW.Checked Then
           Sum = BMW * (BasePrice + options)
           lblCost.Text = FormatCurrency(Sum)
       End If
   End Sub

   Private Sub optBMW_CheckedChanged(sender As Object, e As EventArgs) Handles optBMW.CheckedChanged
       calculate()
   End Sub

   Private Sub chkLeather_CheckedChanged(sender As Object, e As EventArgs) Handles chkLeather.CheckedChanged
       If chkLeather.Checked = True Then
           Sum += Leather
           lblCost.Text = Sum
       End If

       If chkLeather.Checked = False Then
           Sum -= Leather
           lblCost.Text = Sum
       End If


   End Sub

答案 1 :(得分:0)

Const BasePrice As Double = 20000
Public Const BMW As Double = 1.5
Public Const Chevrolet As Double = 1.0
Public Const Leather As Double = 2000
Public Const Sunroof As Double = 1000
Public Const GPS As Double = 2000
Public Const Performance As Double = 3000
Public Const Wheels As Double = 2000

Dim Sum As Double
Dim options As Double


Sub calculate()
    If optBMW.Checked Then
        Sum = BMW * (BasePrice + options)
        lblCost.Text = FormatCurrency(Sum)
    End If
End Sub

Private Sub optBMW_CheckedChanged(sender As Object, e As EventArgs) Handles optBMW.CheckedChanged
    calculate()
End Sub

Private Sub chkLeather_CheckedChanged(sender As Object, e As EventArgs) Handles chkLeather.CheckedChanged
    If chkLeather.Checked = True Then
        options += Leather
        lblCost.Text = options 
    End If

    If chkLeather.Checked = False Then
        options -= Leather
        lblCost.Text = options 
    End If

    calculate()

End Sub
你可以看到

我在复选框中使用了变量选项而不是sum(在应用乘数后,您只会使用 sum )。在所有复选框中执行此操作。

然后在所有复选框和单选按钮中调用计算,以便每次检查和取消选中时都会重新计算。