2011-03-22 26 views
0

好吧,我已經得到了這麼多,如果你運行它,它會做你所要求的。現在當我輸入99999或-99999時,它不會結束。有人能告訴我我做錯了什麼嗎?我想循環,直到前一次抄表輸入-99999的前哨值​​。我不能得到循環權

Sub Main() 
    ' program to compute a consumer’s electric bill. It will calculate the bill for one or more customers 
    ' by looping until a sentinel value of -99999 is entered for the previous meter reading. 
    Dim previousReading As Integer = 0 
    Dim currentReading As Integer = 0 
    Do While (previousReading <> -99999) 
     Dim salesTax As Double 
     ' prompt user to input value for previous reading then convert to integer 
     Console.WriteLine("Enter the value of previous meter reading") 
     previousReading = Convert.ToInt32(Console.ReadLine()) 
     ' prompt user to input value for current reading then convert to integer 
     Console.WriteLine("Enter the value of current meter reading") 
     currentReading = Convert.ToInt32(Console.ReadLine()) 
     Dim kwhConsumed As Integer 
     Dim electricCharge, totalBill As Double 
     ' calculate KWH consumed 
     kwhConsumed = currentReading - previousReading 
     ' Use select case to determine electricCharge 
     Select Case kwhConsumed 
      Case Is < 500 
       electricCharge = kwhConsumed * 0.05 
      Case 500 To 1000 
       electricCharge = 25 + ((kwhConsumed - 500) * 0.055) 
      Case Is > 1000 
       electricCharge = 52.5 + ((kwhConsumed - 1000) * 0.06) 
     End Select 
     ' calculate sales tax 
     salesTax = electricCharge * 0.085 
     ' calculate total charges 
     totalBill = electricCharge + salesTax 
     ' Output values for kwhConsumed, electricCharge, salesTax, and totalBill 
     Console.WriteLine("KWH consumed = " & kwhConsumed & " KWH") 
     Console.WriteLine("Electric charge = $" & Math.Round(electricCharge, 2)) 
     Console.WriteLine("Sales tax = $" & Math.Round(salesTax, 2)) 
     Console.WriteLine("Total bill = $" & Math.Round(totalBill, 2)) 
    Loop 
End Sub 
+0

我跑你的程序和-99999上次閱讀和1輸入的電流讀數。它運行一次並停止。我沒有看到你的問題。 – 2011-03-22 20:20:29

回答

0

您可以嘗試使用字符串比較來代替上一次閱讀<> -99999。你還需要使用絕對值同時考慮-99999 99999做這樣的事情

Do While (previousReading <> 99999) 

    //code 
    previousReading = Math.Abs(Convert.ToInt32(Console.ReadLine())) 
    //code 

Loop 
0

我猜這是作業嗎?

我不知道你是否會考慮在你的語句後插入Debug.Print語句和某種「break」語句。要查找「break」語句,請搜索「vb.net exit loop」並查看彈出的內容。