当值超过100,000+时,Cint溢出错误

时间:2012-01-23 05:11:35

标签: vbscript

我是编程的新手,并且遇到了Cint溢出错误的麻烦。每当值达到100,000+时,我都会收到Cint溢出错误。这是我编程课程的一个练习练习。据我所知,我在实践中完全按照它编写的方式对其进行了编码,但实践表明使用的值高达300,000。有人可以解释我可能做错了吗?

<script language="VBscript">
Option Explicit
DIM numberofshifts, totalshift1, totalshift2, _
  totalshift3, grandtotal, shiftaverage
numberofshifts=3
totalshift1 = Inputbox("How many widgets during the first shift")
totalshift2 = Inputbox("How many widgets during the second shift")
totalshift3 = Inputbox("How many widgets during the third shift")
grandtotal = cint(totalshift1) + totalshift2 + totalshift3
shiftaverage = grandtotal / numberofshifts
Document.write "The Total of the Three Shifts is " & grandtotal
Document.write "<br>The Average of the Three Shifts is " & shiftaverage
</script>

2 个答案:

答案 0 :(得分:37)

CInt可以处理-32,768和32,767之间。

使用CLng代替CInt

MSDN Reference

答案 1 :(得分:2)

将字符串数据转换为整数可以通过使用CInt()CLng()或CDbl()来完成。 记住这些数据类型的大小限制很重要。不同的编程语言有不同的局限性 Here is a link to VBScript Data Types.

整数可以处理从-32,768到32,767的整数。 Long可以处理从-2,147,483,648到2,147,483,647的整数。 双打可以处理高达1.79769313486232E + 308的数字(这个数字比太阳中的原子数大1.19亿十亿。)它们也是双浮点精度;意思是双精度也可以处理非常精确的小数点。

grandtotal = cdbl(totalshift1) + totalshift2 + totalshift3 

这将消除溢出问题。如果用户输入非号码,那么它将无法处理错误,但这是另一个话题。