@喬恩金門: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