Vbscript 我的VB脚本中出现cint溢出错误

Vbscript 我的VB脚本中出现cint溢出错误,vbscript,Vbscript,我是一名编程新手,学习基本的VB脚本编程课程是我获得学位的一项要求。我有一个程序,它将摄氏数转换成法伦海特,并以增量和要查看的数量进行操作。这个程序在可视化逻辑流程图中运行得很好,我花了25个多小时试图让这个程序正常工作。我知道这一定是一件非常愚蠢的事情,但作为编程界的新手,我不知所措,也不知道答案。有人可以看看这个脚本,它是非常基本的,看看是否有什么我错过了。错误总是在FAHTTEMP=(9/5)*TEMPACUM+32的Else之后出现。如有任何建议,将不胜感激。提前感谢您抽出时间。乔恩 O

我是一名编程新手,学习基本的VB脚本编程课程是我获得学位的一项要求。我有一个程序,它将摄氏数转换成法伦海特,并以增量和要查看的数量进行操作。这个程序在可视化逻辑流程图中运行得很好,我花了25个多小时试图让这个程序正常工作。我知道这一定是一件非常愚蠢的事情,但作为编程界的新手,我不知所措,也不知道答案。有人可以看看这个脚本,它是非常基本的,看看是否有什么我错过了。错误总是在FAHTTEMP=(9/5)*TEMPACUM+32的Else之后出现。如有任何建议,将不胜感激。提前感谢您抽出时间。乔恩

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
选项显式
调光温度,调幅,增量
Dim newtemp,Tempacum,fahtemp,loopnum,圣殿骑士
celtemp=inputbox(“您的起始温度是多少?”)
amttemp=输入框(“您希望显示多少温度?”)
增量=输入框(“您想要多少温度增量?”)
当loopnum我们正在显示”&amttemp&“时间”
Document.write“
我们通过以下方式递增:”&increment Document.write“
温度表如下:”&templast
当您试图存储大于数据类型可以处理的值时,会发生溢出错误


如果输入太大,您将遇到此错误

@Jon Gammon:
tempaccum=tempaccum+increment
的计算方式与您想象的不同,它不是像数字一样添加增量,而是像字符串一样将其串联,即,如果开始温度为
10
,增量为
5
,显示次数为
3
,您希望您的输出是

1. Cel Temp: 10 - Fah Temp: 50
2. Cel Temp: 15 - Fah Temp: 59
3. Cel Temp: 20 - Fah Temp: 68
相反,你会得到

1. Cel Temp: 10 - Fah Temp: 50
2. Cel Temp: 105 - Fah Temp: 221
3. Cel Temp: 1055 - Fah Temp: 1931
这就是导致溢出的原因,因为
tempaccum
变得巨大,如果对它进行进一步的乘法运算,就会破坏脚本
Document.Write
也是无效的VBScript代码,因此我将其放入了
MsgBox


这是您的脚本的一份经过测试的有效副本,我对其进行了一些修改,以解决上述问题,并对其进行了一些改进:

Option Explicit
Dim celtemp, amttemp, increment 
Dim tempaccum, fahtemp, loopnum, templist

celtemp   = inputbox("What is your starting temperature?")
amttemp   = inputbox("How many temperatures do you want displayed?")
increment = inputbox("What temperature increments do you want?")

' Formula to converts Celcius to Fahrenheit
Function fahrenheit(ByRef celcius)
    fahrenheit = ((9 / 5)* celcius) + 32
End Function

' Some error checking
If NOT IsNumeric(amttemp) Then 
    amttemp = 1
Else
    amttemp = Fix(amttemp) ' only interested in integer part '
End If

For loopnum = 1 To amttemp
    If loopnum = 1 then
        tempaccum = celtemp
        fahtemp   = fahrenheit(tempaccum)
        templist  = "1. " & "Cel Temp: " & tempaccum & _
                    " - " & "Fah Temp: " & fahtemp & vbCrLf
    Else
        tempaccum = celtemp + (increment * (loopnum - 1))
        fahtemp   = fahrenheit(tempaccum)
        templist  = templist & loopnum & ". " & _
                    "Cel Temp: " & tempaccum & " - " & _
                    "Fah Temp: " & fahtemp & vbCrLf
    End If
Next

MsgBox "Starting at temperature: " & celtemp & vbCrLf & _
"Displaying " &amttemp & " times." & vbCrLf & _
"Incrementing by: " &increment & vbCrLf & vbCrLf & _
"The temperature table is as follows: " & vbCrLf & templist, 0, _
"Temperature Converter"


更新

我的类非常基本,我们没有使用函数,实际上我们只做了输入,而循环,案例选择和If。所以我已经忙得不可开交了。虽然你的工作非常出色,但我感到震惊的是,它比我们现在的水平要先进一点

我明白了,好吧,我回到了原始脚本,只更新了破坏它的部分,如前所述这是新的工作副本

Option Explicit
Dim celtemp, amttemp, increment 
Dim tempaccum, fahtemp, loopnum, templist

celtemp   = inputbox("What is your starting temperature?")
amttemp   = inputbox("How many temperatures do you want displayed?")
increment = inputbox("What temperature increments do you want?")

loopnum = 1

Do While loopnum < CInt(amttemp)
    If loopnum = 1 Then
        tempaccum = celtemp
        fahtemp   = ((9 / 5) * tempaccum) + 32
        templist  = "1. Cel Temp: " & tempaccum & " - " & "Fah Temp: " & fahtemp & vbCrLf
    Else
        tempaccum = celtemp + (increment * loopnum)
        fahtemp   = ((9 / 5) * tempaccum) + 32
        templist  = templist & loopnum & ". Cel Temp: " & tempaccum & " - " & "Fah Temp: " & fahtemp & vbCrLf
    End If

    loopnum = loopnum + 1
Loop

MsgBox "Starting at temperature: " & celtemp & vbCrLf & _
"Displaying " & amttemp & " times." & vbCrLf & _
"Incrementing by: " & increment & vbCrLf & vbCrLf & _
"The temperature table is as follows: " & vbCrLf & templist, 0, _
"Temperature Converter"
选项显式
调光温度,调幅,增量
Dim Tempacum、fahtemp、loopnum、圣殿骑士
celtemp=inputbox(“您的起始温度是多少?”)
amttemp=输入框(“您希望显示多少温度?”)
增量=输入框(“您想要多少温度增量?”)
loopnum=1
当loopnum
好的,我有那部分。所以我认为这与它不断出现故障的fahtemp线路和其他东西没有任何关系。我之所以这么说,是因为如果我输入0作为起始温度,5作为增量和数量,我仍然会得到相同的错误。如果你所说的是正确的,那么0-20不可能是一个足够高的值,导致它失败。它必须与循环或tempaccum内存位置有关。谢谢你的邀请reply@Jon金门:请参阅下面的我的答案以获得解释和修复。在您最后一次评论之后,我对我的答案添加了更新。希望能有所帮助。:-)棒极了,效果非常好,它与我们在课堂上的表现保持一致。非常感谢你的帮助。我离得很近,只是缺少一些关键部分。谢谢你帮我看。乔恩