@乔恩金门: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"
我你最后的意见后添加一个更新我的答案。希望能帮助到你。 :-) – stealthyninja 2011-04-14 14:02:07