[英]Basic Visual Basic Program Error - Probably Variable Error
我正在为Visual Basic编写这个程序,它将确定一个基于用水量的账单。 我的问题是我输入的所有值在命令提示符中都返回为零。 任何人都可以解释这段代码有什么问题吗?
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
首先,你的声明:
If (1000 > waterusage) and (waterusage < 2000) then
相当于:
If (waterusage < 1000) and (waterusage < 2000) then
这意味着它是在测试waterusage
既低于1000 和小于2000(即,只是它是小于1000)少。 我想你可能有以下几点意思:
If (waterusage > 1000) and (waterusage <= 2000) then
你会注意到我已经使用了<=
因为你的if
语句根本不处理边缘情况(2000年既不低于,也不高于2000,所以输入2000会导致你的原始if
语句都没有被触发)。
您还需要对0 to 1000
和2000 to 3000
案例进行类似的更改。
我也不完全确定:
:
End If
else if ...
构建正确的是(除非VB.NET 急剧在较低的水平,因为VB6天改变(我知道有很多变化,但是这种变化的一个低层次的东西的工作if
是不太可能)。一end if
据我所知,关闭整个 if
语句,所以else
应该在 if
和end if
。
所以我会看到类似的东西:
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
该代码还有一些小的修复,如正确指定I / O的Out
和In
,并使用“正确的”大小写,虽然它没有经过全面测试,可能仍然有一些语法错误。 代码背后的想法(基本上是if
语句)仍然是合理的。
但是,您可能需要检查您的规格。
当公用事业公司对他们的资源收费时,他们倾向于对超出一定水平的超额征收更高的费率,而不是整个金额。 换句话说,我希望看到的第一千立方英尺,然后在每个立方英尺超出了1.75美分,这将使您的语句看起来更像是收费的$ 15:
ElseIf (WaterUsage > 1000) and (WaterUsage <= 2000) then
TotalCharge = 0.0175 * (WaterUsage - 1000) + 15
在这种情况下这是有道理的,因为你的第一千的收费为1.5c / ft 3 ,第二千的收费为1.75c / ft 3 ,第三千的收费为2c / ft 3 ,下限为15美元(无论您实际使用多少,您都会被收取第一千元的费用;对于使用超过三千立方英尺(类别的罚款率)的任何人来说,固定费用为70美元。
但是,根据经验,这是我的假设。 可能是您的规格另有说明,在这种情况下可以随意忽略本节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.