2011-04-13 93 views
0

我是新手编程和基本VB脚本编程课程,作为我正在学习的学位课程的要求。我有这个程序,将摄氏数字转换为farhenheit,并以增量和数量来查看。这个程序在可视逻辑流程图中工作得很好,我花了超过25个小时只是试图让这个程序运行。我知道它必须是一些完全愚蠢的东西,但是对于编程界来说是新的,我感到茫然,并且没有答案。有人可以看看这个脚本,这是非常基本的,看看是否有我缺少的东西。错误总是在fahtemp =(9/5)* tempaccum + 32之后出现。任何暗示都将不胜感激。预先感谢您的时间。 Jon在我的VB脚本中发生cint溢出错误

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 
+0

我你最后的意见后添加一个更新我的答案。希望能帮助到你。 :-) – stealthyninja 2011-04-14 14:02:07

回答

1

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

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

+0

好吧,我有那个部分。所以我认为它与fahtemp系列没有任何关系,因为它一直存在失败以及其他问题。我只是这样说,因为如果我为我的起始温度输入0,并且两个增量和多少输入5,我仍然会得到相同的错误。如果你说的话是真的,那么0-20就不会是一个足够高的值来导致它失败。它必须与循环或温度记忆的地方有关。感谢您的回复 – 2011-04-13 18:00:10

+0

@Jon Gammon:请参阅下面的答案以获得解释和修复。 – stealthyninja 2011-04-13 19:27:49

1

@乔恩金门: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循环,Ca se选择和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" 
+0

真棒,它很好,它保持在我们在课堂上的地方。非常感谢你的帮助。我很接近,只是缺少一些关键部分。感谢您为我着想。乔恩 – 2011-04-14 14:26:07