ich bin auch ein Neuling, sicher, dies ist eine schreckliche Art und Weise zu codieren, aber es scheint zu funktionieren ... zunächst einmal ersetzen Literale mit Variablen helfen zu beheben sich ändernde Anforderungen berücksichtigen. Ich habe nicht viele Fälle überprüft, also ist das vielleicht nicht genau, aber es hat Spaß gemacht, an das Problem zu denken.
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
Willkommen bei Stack Overflow! Es sieht so aus, als müssten Sie lernen, einen Debugger zu verwenden. Bitte helfen Sie sich selbst [https://ericlippert.com/2014/03/05/how-to-debug-small-programs/]. Wenn Sie danach noch Probleme haben, können Sie gerne weitere Einzelheiten erfahren. –
Sie überprüfen nicht 30 oder 60. –