2011-04-13 55 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