Visual Basic - 开始程序代码

时间:2014-02-17 07:39:44

标签: vb.net

对于包A,我的输出总是14.95。其他任何东西似乎都工作正常。

对于我需要的程序:

互联网服务提供商向其客户提供3个订阅套餐,并为非营利组织提供折扣

  • packaga答:10个小时的访问费用为每月9.95美元。额外的是每小时2.00美元。
  • 套餐B:20个小时的访问量,每月14.95美元。额外的是每个
  • $ 1.00
  • 套餐C:无限制使用,每月19.95美元
  • 公益组织:如果用户选择公益组织复选框a 20 应从最终费用中扣除%折扣。 输入验证“:如果一个月内使用的小时数不能超过744,则该数字必须为数字

btnCalc_Click

    Public Class Form1
    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
        'Declare variables and constant

        Dim decTotal As Decimal
        Dim intHours As Integer
        Const decNonProfit As Decimal = 0.8D
     Try

            intHours = CInt(txtHoursUsed.Text)

            If CInt(txtHoursUsed.Text) > 744 Then
                MessageBox.Show("Monthly Hours Can't Exceed 744")
                txtHoursUsed.Text = String.Empty
                lblTotalDue.Text = String.Empty
            End If

            'Calculate A Package Total without discount
            If radPackageA.Checked = True And intHours > 10 Then
                decTotal = (9.95 + ((intHours - 10) * 2))
            ElseIf intHours <= 10 Then
                decTotal = 9.95
            End If

            'Calculate B Package Total without discount
            If radPackageB.Checked = True And intHours > 20 Then
                decTotal = (14.95 + ((intHours - 20) * 1))
            ElseIf intHours <= 20 Then
                decTotal = 14.95
            End If

            'Calculate C Package Total without discount
            If radPackageC.Checked = True Then
                decTotal = 19.95
            End If

            'Calculate Total for packages if Nonprofit is checked
            If chkNonProfit.Checked = True Then
                decTotal = decTotal * decNonProfit

            End If

            'Show total due
            lblTotalDue.Text = decTotal.ToString("c")

        Catch ex As Exception
            MessageBox.Show("Input Error")

        End Try

    End Sub

btnClear_Click

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        radPackageA.Checked = False
        radPackageB.Checked = False
        radPackageC.Checked = False
        chkNonProfit.Checked = False
        lblTotalDue.Text = String.Empty
        txtHoursUsed.Text = String.Empty
    End Sub

btnExit_Click

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    End Class

2 个答案:

答案 0 :(得分:2)

您需要在选择检查中附上整个条件。发生的事情是,当repackageB.Checked为false时,始终会运行您的错误条件。

            'Calculate A Package Total without discount
            If radPackageA.Checked Then
                If intHours > 10 Then
                    decTotal = (9.95 + ((intHours - 10) * 2))
                ElseIf intHours <= 10 Then
                    decTotal = 9.95
                End If
            End If

            'Calculate B Package Total without discount
            If radPackageB.Checked Then
                If intHours > 20 Then
                    decTotal = (14.95 + ((intHours - 20) * 1))
                ElseIf intHours <= 20 Then
                    decTotal = 14.95
                End If
            End If

            'Calculate C Package Total without discount
            If radPackageC.Checked Then
                decTotal = 19.95
            End If

答案 1 :(得分:1)

您正在使用ElseIf以错误的方式分解条件。 按照您的代码,您每次dectTotal = 14.95radPackageB.Checked = False

都会获得intHours <= 20

应该是这样的:

        If radPackageB.Checked Then
            If intHours > 20 Then
                decTotal = (14.95 + ((intHours - 20) * 1))
            ElseIf intHours <= 20 Then
                decTotal = 14.95
            End If
        End If

所以请随意接受Mark的回答