2014-09-28 54 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