我的 VB 脚本中出现 cint 溢出错误
我是编程新手,并且将基础 VB 脚本编程课程作为我正在攻读的学位的要求。我有这个程序,可以将摄氏度转换为华氏度,并以增量和要查看的量进行转换。这个程序在视觉逻辑流程图中工作得很好,我花了超过 25 个小时试图让这个程序工作。我知道这一定是完全愚蠢的事情,但作为编程世界的新手,我很茫然,没有答案。有人可以看一下这个脚本,它非常基本,看看是否有我遗漏的东西。该错误总是出现在 Else at fahtemp = (9/5) * tempaccum + 32 之后。任何 insite 将不胜感激。预先感谢您抽出时间。乔恩
Option Explicit
Dim celtemp, amttemp, increment
Dim newtemp, tempaccum, fahtemp, loopnum, templist
celtemp = inputbox("What is your starting Temp?")
amttemp = inputbox("How many temperatures do you want displayed?")
increment = inputbox("What temperature increments do you want?")
Do while loopnum < amttemp
loopnum = loopnum +1
If loopnum = 1 then
tempaccum = celtemp
fahtemp = (9/5) * (tempaccum) +32
templist = "1." & "Cel Temp: " &tempaccum & "- " & "Fah Temp: " &fahtemp
else
tempaccum = tempaccum + increment
fahtemp = (9/5) * tempaccum + 32
templist = templist &" " &loopnum & "." & "Cel Temp: " &tempaccum & "- " & "Fah Temp: " &fahtemp
End If
newtemp = celtemp + increment
Loop
Document.write "We are starting at Temp: " &celtemp
Document.write "<br> We are displaying " &amttemp & "times."
Document.write "<br> We are incrementing by: " &increment
Document.write "<br> The Temperature Table is as follows: " &templist
I am new to programming and taking a basic VB script programming class as a requirement for the degree I am working on. I have this program that is converting celsius numbers to farhenheit and doing it in increments and the amount to be viewed. This program works in visual logic flow chart just fine, and I have spent more than 25 hours just trying to get this program to work. I know it has to be something completely stupid, but being new to the programming world I am at a loss, and don't have the answers. Could someone look at this script, it is very basic and see if there is something I am missing. The error always comes up after the Else at fahtemp = (9/5) * tempaccum + 32. Any insite would be greatly appreciated. Thank you in advance for your time. Jon
Option Explicit
Dim celtemp, amttemp, increment
Dim newtemp, tempaccum, fahtemp, loopnum, templist
celtemp = inputbox("What is your starting Temp?")
amttemp = inputbox("How many temperatures do you want displayed?")
increment = inputbox("What temperature increments do you want?")
Do while loopnum < amttemp
loopnum = loopnum +1
If loopnum = 1 then
tempaccum = celtemp
fahtemp = (9/5) * (tempaccum) +32
templist = "1." & "Cel Temp: " &tempaccum & "- " & "Fah Temp: " &fahtemp
else
tempaccum = tempaccum + increment
fahtemp = (9/5) * tempaccum + 32
templist = templist &" " &loopnum & "." & "Cel Temp: " &tempaccum & "- " & "Fah Temp: " &fahtemp
End If
newtemp = celtemp + increment
Loop
Document.write "We are starting at Temp: " &celtemp
Document.write "<br> We are displaying " &amttemp & "times."
Document.write "<br> We are incrementing by: " &increment
Document.write "<br> The Temperature Table is as follows: " &templist
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您尝试存储大于数据类型可以处理的值时,就会发生溢出错误。
如果您的输入太大,您将遇到此错误。
An overflow error occurs when you are attempting to store a value greater than the data type can handle.
If your inputs are too large, you will encounter this error.
@Jon Gammon:
tempaccum = tempaccum +increment
并不像您想象的那样进行计算,而是像数字一样添加增量,而是像字符串一样连接它,即如果起始温度是10
,增量为5
,显示次数为3
,您期望的输出是相反,您会得到
这就是什么导致溢出的原因是
tempaccum
变得巨大,对其进行进一步的乘法会破坏脚本。Document.Write
也不是有效的 VBScript 代码,因此我将其放入MsgBox
中。这是您的脚本的经过测试的工作副本,我对其进行了一些重写,以解决上述问题并对其进行了一些改进:
更新
我明白了,好吧,我已经回到原来的脚本,只更新了破坏它的部分,如前所述。 这是新的工作副本:
@Jon Gammon:
tempaccum = tempaccum + increment
isn't calculating as you might think it would, instead of adding the increment like it's a number, it was concatenating it like a string, i.e. if starting temperature is10
, increment is5
and number of times to display is3
, you'd expect your output to beInstead you would get
This is what caused the overflow because
tempaccum
became huge and further multiplications on it would break the script.Document.Write
also isn't valid VBScript code, so I made that into aMsgBox
.Here's a working and tested copy of your script which I rewrote a little to fix the above-mentioned issues and to improve it a little too:
Update
I see, ok, I've gone back to the original script and only updated the parts that were breaking it, as previously mentioned. Here's the new working copy: