Reputation: 173
I am writing this program for visual basic that will determine a bill based off water usage. My problem is that all values I input come back as zero in the command prompt. Can anyone explain what's wrong with this code?
Option Explicit On
Option Strict On
Imports System
module eurekawatercompany
Sub Main ()
' Declare variables of problem
Dim waterusage as double
Dim totalcharge as double
' Prompts for user to enter their water usage.
Console.write ("Please enter your current water usage (cubic feet): ")
waterusage = convert.toint32(console.readline())
If (waterusage < 1000) then
totalcharge = 15
End If
If (1000 > waterusage) and (waterusage < 2000) then
totalcharge = 0.0175 * waterusage + 15
End If
else if (2000 < waterusage) and (waterusage > 3000) then
totalcharge = 0.02 * waterusage + 32.5
End If
' 32.5 is the price of exactly 2000cm^(3) of water
else if (waterusage > 3000) then
totalcharge = 70
End If
Console.out.writeline ("Total charge is: ")
Console.out.writeline (totalcharge)
End sub
End Module
Upvotes: 1
Views: 561
Reputation: 881113
First up, your statement:
If (1000 > waterusage) and (waterusage < 2000) then
is equivalent to:
If (waterusage < 1000) and (waterusage < 2000) then
meaning that it's testing that waterusage
is both less than 1000 and less than 2000 (ie, just that it's less than 1000). I think you may have meant something along the lines of:
If (waterusage > 1000) and (waterusage <= 2000) then
You'll notice I've used <=
as well since your if
statements don't handle the edge cases at all (2000 is neither less than, nor greater than, 2000 so entering 2000 would cause none of your original if
statements to fire).
You'll also need to make similar changes to the 0 to 1000
and 2000 to 3000
cases.
I'm also not entirely certain that the:
:
End If
else if ...
construct is correct (unless VB.NET has changed drastically at the lower levels since the VB6 days (I know that there were a lot of changes but changing such a low-level thing as the workings of if
would be unlikely). An end if
closes off the entire if
statement as far as I'm aware, so the else
should be between the if
and end if
.
So I'd be looking at something like:
Option Explicit On
Option Strict On
Imports System
Module EurekaWaterCompany
Sub Main ()
Dim WaterUsage as double
Dim TotalCharge as double
Console.Out.Write ("Please enter your current water usage (cubic feet): ")
WaterUsage = Convert.ToInt32 (Console.In.ReadLine())
If (WaterUsage <= 1000) then
TotalCharge = 15
ElseIf (WaterUsage > 1000) and (WaterUsage <= 2000) then
TotalCharge = 0.0175 * WaterUsage + 15
ElseIf (Waterusage > 2000) and (WaterUsage <= 3000) then
TotalCharge = 0.02 * WaterUsage + 32.5
Else
TotalCharge = 70
End If
Console.Out.WriteLine ("Total charge is: ")
Console.Out.WriteLine (TotalCharge)
End sub
End Module
That code also has some minor fixes like properly specifying Out
and In
for the I/O, and using "proper" capitalisation, although it's not fully tested and may still have some syntax errors. The idea behind the code (basically the if
statement) is still sound.
However, you may want to check your specifications.
When utilities charge for their resource, they tend to levy higher rates on the excess beyond a certain level, not the entire amount. In other words, I'd expect to see a charge of $15 on the first 1000 cubic feet and then 1.75 cents on each cubic foot beyond that, which would make your statements look more like:
ElseIf (WaterUsage > 1000) and (WaterUsage <= 2000) then
TotalCharge = 0.0175 * (WaterUsage - 1000) + 15
That would make sense in this situation since you'd be charged 1.5c/ft3 for the first thousand, 1.75c/ft3 for the second thousand, and 2c/ft3 for the third thousand, with a lower limit of $15 (you get charged for the first thousand no matter how much you actually use) and a fixed charge of $70 for anyone who uses more than three thousand cubic feet (sort of a penalty rate).
But, that's supposition on my part, from experience. It may be that your specifications say otherwise, in which case feel free to ignore this section.
Upvotes: 3