2016-12-11 4 views
-3
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 verwendet 75m3 Wasser pro Monat, so dass er zahlen muss: 30 x 120 = 3600 30 x 150 = 4500 15 x 15 = 2850 und gesamt: 10950Newbie visual basic 6

aber mein Code tut mir richtig es im Neuling auf

+1

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. –

+0

Sie überprüfen nicht 30 oder 60. –

Antwort

2

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 
+0

Danke oder Ihre Hilfe, aber es scheint nicht funktionieren auf meinem Vb6? Ich weiß nicht, warum, sollte ich die neue Textbox hinzufügen und nennen sie Balance? –

Verwandte Themen