2016-12-11 7 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: 30×120 = 3600 30×150 = 4500 15×15 = 2850 と合計:10950彼は支払わなければならないので、 Aさんは水の75立方メートル月を使用 初心者のVisual Basic 6

が、私のコードのdoesntのは、右の私は、私も初心者だそれをイム初心者

+1

ようこそスタックオーバーフロー!デバッガの使い方を学ぶ必要があるようです。 [補完的なデバッグ手法](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)にご協力ください。その後も問題が残っている場合は、もう少し詳しくお聞かせください。 –

+0

あなたは30または60をチェックしません。 –

答えて

2

を修正するのに役立つ、確かこれはそれをコーディングする恐ろしい方法があるが、最初にオフに変数とリテラルを置き換える...動作するようです要件の変更を可能にする。私は多くのケースをチェックしなかったので、これは正確ではないかもしれませんが、問題を考えるのは楽しいものでした。

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

ありがとう、または助けてください、それは私のvb6で動作していないようですか?なぜ私は新しいテキストボックスを追加してバランスを付けるべきなのでしょうか? –