I cant get the loop right
Ok I have got this far and if you run it, it will do what you ask. Now when I type in 99999 or -99999 it will not end. Can someone tell me what I am doing wrong. I am suppose to loop until a sentinel value of -99999 is enter for previous meter reading.
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(开发者_C百科))
' 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
You can try using string comparison instead for previousReading <> -99999. You also need to use absolute value to consider both -99999 and 99999. Do something like this
Do While (previousReading <> 99999)
//code
previousReading = Math.Abs(Convert.ToInt32(Console.ReadLine()))
//code
Loop
I'm guessing this is homework?
Instead of blurting out the answer, I wonder if you might think about inserting a Debug.Print statement and some kind of "break" statement after your previousReading = Convert.ToInt32
statement. To look for the "break" statement, search for "vb.net exit loop" and see what pops up.
精彩评论