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

发布于 2024-11-01 06:08:50 字数 1388 浏览 0 评论 0原文

我是编程新手,并且将基础 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云巢 2024-11-08 06:08:50

当您尝试存储大于数据类型可以处理的值时,就会发生溢出错误。

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

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.

清晰传感 2024-11-08 06:08:50

@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"

更新

我的课程非常基础,我们没有使用过函数,实际上我们所做的唯一的事情就是输入、while 循环、Case Select 和 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"

@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 is 10, increment is 5 and number of times to display is 3, you'd expect your output to be

1. Cel Temp: 10 - Fah Temp: 50
2. Cel Temp: 15 - Fah Temp: 59
3. Cel Temp: 20 - Fah Temp: 68

Instead you would get

1. Cel Temp: 10 - Fah Temp: 50
2. Cel Temp: 105 - Fah Temp: 221
3. Cel Temp: 1055 - Fah Temp: 1931

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 a MsgBox.

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:

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"

Update

My class is pretty basic, we haven't worked with Functions, and really the only thing we have done is inputs, while loops, Case Select and If's. SO I have had my hands full trying to code this out. Although yours works brilliantly, I am affraid it is a little more advanced than where we are at.

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:

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文