2014-09-28 50 views
-1

家庭作业问题:互联网服务提供商向其客户提供三种订阅套餐,另外 为非营利组织提供折扣: a。套餐A:每月9.95美元的访问10小时。额外的时间是每小时$ 2.00 。 b。套餐B:每月14.95美元的20小时访问。额外的时间是 每小时1.00美元。 c。套餐C:每月19.95美元无限次使用。 d。非营利组织:服务提供商为所有非营利组织提供 所有软件包20%的折扣。 用户应该选择客户购买的包装(从一组无线电 按钮)并输入使用的小时数。一个复选框标题为Nonprofit 组织也应该出现在表单上。应用程序应计算并显示应付总金额。如果用户选择非营利组织支票 ,则应从最终费用中扣除20%的折扣。执行 注意:必须使用符号常量 (使用Const关键字)声明所有费率,限制和折扣。VB ElseIf陈述不起作用

使用以下数据,以确定应用程序是否正确地计算: 打包和小时月费 包A,5小时,非营利$ 7.96 包A 25小时$ 39.95 包B,10小时,非营利$ 11.96 套餐B 25小时$ 19.95 套餐C,18小时,非营利$ 15.96 包装℃,25小时$ 19.95

我的代码:

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click 
    'Declare input and output variables 
    Dim intHoursUsed As Integer 
    Dim decTotalDue As Decimal 

    'Calculate Price if Package A is selected. No discount applied. 
    If radPackageA.Checked = True Then 
     If intHoursUsed < 10 Then 
      decTotalDue = CDec(9.95) 
     ElseIf intHoursUsed > 10 Then 
      decTotalDue = CDec((9.95) + ((intHoursUsed - 20) * 2)) 
     End If 
    End If 

    'Calculate Price if Package B is selected. No discount applied. 
    If radPackageB.Checked = True Then 
     If intHoursUsed <= 20 Then 
      decTotalDue = CDec(14.95) 
     ElseIf intHoursUsed > 20 Then 
      decTotalDue = CDec((14.95) + ((intHoursUsed - 20) * 1)) 
     End If 
    End If 

    'Calculate Price if Package C is selected. No discount applied. 
    If radPackageC.Checked = True Then 
     decTotalDue = CDec(19.95) 
    End If 

    'Declare named constant for Nonprofit Discount rate (0.8) 
    Const Nonprofit As Decimal = CDec(0.8) 

    'Add and calculate discount if checkbox is checked. 
    If chkNonprofit.Checked = True Then 
     decTotalDue = CDec(decTotalDue * Nonprofit) 
    End If 

    'Display Total Amount Due in label as string in currency format 
    lblTotalDue.Text = decTotalDue.ToString("c") 

    'Display Error Message it Hours exceed 744 
    If CInt(txtHoursUsed.Text) > 744 Then 
     MessageBox.Show("Please try again. Value must be a numeric inter and must not exceed 744.") 
     txtHoursUsed.Clear() 
     lblTotalDue.Clear() 
    End If 
End Sub 

当我计算A套餐和B套餐时,无论有多少小时,答案仅为9.95美元和14.95美元。请帮忙!我很沮丧。我无法找到什么是错的(我是一个noob)。

+0

你能格式化你的代码吗?阅读起来并不容易。也欢迎[so],请[参观]! – Unihedron 2014-09-28 04:06:45

+0

你在哪里为** intHoursUsed变量赋值**? – har07 2014-09-28 05:18:33

回答

0

你从你的程序得到的答复是非常合情合理的,如果,显然,你没有将值分配给intHoursUsed变量的任何地方:

Dim intHoursUsed As Integer = CInt(txtHoursUsed.Text) 
...... 

在这种情况下,intHoursUsed值默认为0,则intHoursUsed < 10intHoursUsed <= 20总是如此。这样,正如你所说的,答案总是9.95美元和14.95美元,不管它有多少个小时。

+0

Thankyou SOOOOOO! – R4g323 2014-09-28 05:46:12

0

答案可能会通过格式显示,正如Unihedron在上面所述。

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click 
'Declare input and output variables 
Dim intHoursUsed As Integer 
Dim decTotalDue As Decimal 

'Get the total hours 
intHoursUsed = CInt(txtHoursUsed.Text) 

'Calculate Price if Package A is selected. No discount applied. 
If radPackageA.Checked = True Then 
    If intHoursUsed <= 10 Then 
     decTotalDue = CDec(9.95) 
    ElseIf intHoursUsed > 10 Then 
     decTotalDue = CDec((9.95) + ((intHoursUsed - 20) * 2)) 
    End If 
/* Missing End If here */ 

    'Calculate Price if Package B is selected. No discount applied. 
    If radPackageB.Checked = True Then 
     If intHoursUsed <= 20 Then 
      decTotalDue = CDec(14.95) 
     ElseIf intHoursUsed > 20 Then 
      decTotalDue = CDec((14.95) + ((intHoursUsed - 20) * 1)) 
     End If 
    End If 
/*Rest omitted for brevity */ 

失踪End If使一切通过第一检查,radPackageA.Checked = True过滤。如果不是这样,那么除此以外的代码都不会被查看。适当的代码格式化可能会帮助你更快地找到它,阅读你的代码的人也会很感激它。

+1

'如果radPackageA.Checked = True然后'可以简化为'如果radPackageA.Checked然后' – OneFineDay 2014-09-28 04:22:34

+0

好吧,我做了你所说的但我仍然有同样的问题。这就像它忽略了Elseif的陈述。我甚至尝试了其他我在网上找到的代码,例如:http://www.dreamincode.net/forums/topic/49023-help-with-assignment/,仍然是同样的问题。当我点击计算时,无论我输入多少小时,它都会给我&9。A为95,B为$ 14.95 – R4g323 2014-09-28 04:54:05

+0

您需要指定intHoursUsed。目前,如果这是您的代码的整体,那么您也错过了这一点。看到我上面的编辑。 – tjcertified 2014-09-28 05:23:09