我不能让循环正确

时间:2011-03-22 20:08:54

标签: vb.net

好的,我已经做到了这一点,如果你运行它,它会做你所要求的。现在,当我输入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

2 个答案:

答案 0 :(得分:0)

我猜这是家庭作业?

我想知道你是否可以考虑在previousReading = Convert.ToInt32语句之后插入一个Debug.Print语句和某种“break”语句,而不是脱口而出。要查找“break”语句,请搜索“vb.net exit loop”并查看弹出的内容。

答案 1 :(得分:0)

您可以尝试使用字符串比较代替previousReading&lt;&gt; -99999。您还需要使用绝对值来考虑-99999和99999.执行类似这样的操作

Do While (previousReading <> 99999) 

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

Loop
相关问题