Private Sub Command1_Click()
Select Case used.Text
Case Is <= 30
pay = used * 120
Case Is > 30, Is <= 60
pay = used * 150
Case Is > 60, Is <= 90
pay = used * 190
End Select
EX: MR.A每月使用75立方米的水,所以他必须支付: 30 x 120 = 3600 30 x 150 = 4500 15 x 15 = 2850 总计:10950
但是我的代码没有正确帮我解决它新手
答案 0 :(得分:1)
我也是一个新手,确定这是一种可怕的编码方式,但它似乎有效...首先用变量替换文字以允许更改要求。我没有检查很多情况,所以这可能不准确,但通过问题思考很有趣。
Private Sub Command1_Click()
Dim pay As Double
Dim used As Double
Dim balance As Double
Dim BillingIncrement As Double
Dim FirstUnitPrice As Double
Dim SecondUnitPrice As Double
Dim MaxUnitPrice As Double
BillingIncrement = 30
FirstUnitPrice = 120
SecondUnitPrice = 150
MaxUnitPrice = 190
'put in loop to test various inputs for debug only
For used = 10 To 100 Step 5
Debug.Print "used ", used
If used <= BillingIncrement Then
pay = used * FirstUnitPrice
Else
pay = BillingIncrement * FirstUnitPrice
Debug.Print "first " & BillingIncrement & " units billed at 120"
balance = used - BillingIncrement
Debug.Print "balance ", balance
If balance > BillingIncrement Then
pay = pay + BillingIncrement * SecondUnitPrice
Debug.Print "second " & BillingIncrement & " units billed at " & SecondUnitPrice
balance = balance - BillingIncrement
If balance > 0 Then
pay = pay + balance * MaxUnitPrice
Debug.Print balance, " units billed at " & MaxUnitPrice
End If
Else
Debug.Print balance, " billed at " & SecondUnitPrice
pay = pay + balance * SecondUnitPrice
End If
End If
Debug.Print "Pay = ", pay
' a couple example test cases
If used = 40 Then
If pay <> 5100 Then
Debug.Print "error"
Else: Debug.Print "ok so far"
End If
End If
If used = 60 Then
If pay <> 8100 Then
Debug.Print "error"
Else: Debug.Print "ok so far"
End If
End If
'reset for next loop
pay = 0
balance = 0
Next
End Sub