我的VB脚本中的cint溢出错误 [英] cint overflow error in my VB script

查看:37
本文介绍了我的VB脚本中的cint溢出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,我正在学习基本的 VB 脚本编程课程作为我所攻读学位的要求.我有这个程序将摄氏度数转换为华氏度,并以增量和要查看的数量进行转换.这个程序在可视化逻辑流程图中工作得很好,我花了超过 25 个小时才试图让这个程序工作.我知道这一定是完全愚蠢的事情,但是作为编程世界的新手,我不知所措,并且没有答案.有人可以看看这个脚本,它是非常基本的,看看是否有我遗漏的东西.错误总是在 fahtemp = (9/5) * tempaccum + 32 的 Else 之后出现.任何现场都将不胜感激.提前感谢您的时间.乔恩

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

推荐答案

@Jon Gammon: tempaccum = tempaccum + increment 并没有像你想象的那样计算,而是像这样添加增量它是一个数字,它像字符串一样将它连接起来,即如果起始温度为 10,增量为 5,显示次数为 3>,你希望你的输出是

@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

相反,你会得到

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.

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

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"



更新

我的课程非常基础,我们还没有使用过函数,实际上我们所做的唯一一件事就是输入、while 循环、Case Select 和 If.所以我已经忙得不可开交,试图将其编码出来.虽然你的工作很出色,但我担心它比我们现在的位置更先进.

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"

这篇关于我的VB脚本中的cint溢出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆