2010-06-07 45 views
0

我有一個在Visual Basic中執行的項目。 我的問題是,結果總是錯誤的(例如,而不是2011年它給出2000)。而且我不能將Inch(1)或feet(3)設置爲期望單位,它會給出Infinity錯誤。如果我把原始單位和期望單位放在英寸(1)中,結果是「不是數字」。單位轉換應用程序需要的數組幫助

這是我迄今爲止所做的代碼。該項目是關於數組。任何幫助讚賞。

公共類Form1中

Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click 

    Dim original(9) As Long 
    Dim desired(9) As Long 
    Dim a As Integer 
    Dim o As Integer 
    Dim d As Integer 
    Dim inch As Long, fathom As Long, furlong As Long, kilometer As Long 
    Dim meter As Long, miles As Long, rod As Long, yard As Long, feet As Long 

    a = Val(Input3.Text) 
    o = Val(Input1.Text) 
    d = Val(Input2.Text) 

    inch& = 0.0833 
    rod& = 16.5 
    yard& = 3 
    furlong& = 660 
    meter& = 3.28155 
    kilometer& = 3281.5 
    fathom& = 6 
    miles& = 5280 

    original(1) = inch 
    original(2) = fathom 
    original(3) = feet 
    original(4) = furlong 
    original(5) = kilometer 
    original(6) = meter 
    original(7) = miles 
    original(8) = rod 
    original(9) = yard 

    desired(1) = inch 
    desired(2) = fathom 
    desired(3) = feet 
    desired(4) = furlong 
    desired(5) = kilometer 
    desired(6) = meter 
    desired(7) = miles 
    desired(8) = rod 
    desired(9) = yard 

    If o < 1 Or o > 9 Or d < 1 Or d > 9 Then 
     MessageBox.Show("Units must range from 1-9.", "Error", _ 
     MessageBoxButtons.OK, _ 
     MessageBoxIcon.Information) 
     Return 
    End If 

    Output.Text = (a * original(o))/desired(d) 

End Sub 

末級

回答

1

GEIA SOU馬諾利,

通常數組是從零開始的。 英寸,米等應聲明爲雙倍,不長。

該代碼會做的皈依corectly

Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click 
    Dim original(8) As Double 
    Dim desired(8) As Double 
    Dim a As Integer 
    Dim o As Integer 
    Dim d As Integer 
    Dim inch As Double, fathom As Long, furlong As Double, kilometer As Double 
    Dim meter As Double, miles As Long, rod As Double, yard As Double, feet As Long 

    a = CInt(Val(Input3.Text)) 
    o = CInt(Val(Input1.Text)) 
    d = CInt(Val(Input2.Text)) 

    inch = 0.0833333333 
    rod = 16.5 
    yard = 3 
    furlong = 660 
    meter = 3.2808399 
    kilometer = 3280.8399 
    fathom = 6 
    miles = 5280 

    original(0) = inch 
    original(1) = fathom 
    original(2) = feet 
    original(3) = furlong 
    original(4) = kilometer 
    original(5) = meter 
    original(6) = miles 
    original(7) = rod 
    original(8) = yard 

    desired(0) = inch 
    desired(1) = fathom 
    desired(2) = feet 
    desired(3) = furlong 
    desired(4) = kilometer 
    desired(5) = meter 
    desired(6) = miles 
    desired(7) = rod 
    desired(8) = yard 

    If o < 0 Or o > 8 Or d < 0 Or d > 8 Then 
     MessageBox.Show("Units must range from 0-8.", "Error", _ 
     MessageBoxButtons.OK, _ 
     MessageBoxIcon.Information) 
     Return 
    End If 

    Output.Text = CStr((a * original(o))/desired(d)) 

End Sub 
+0

你也可以只使用原始數組。所需的數組不需要。 例如: Output.Text = CStr((a * original(o))/ original(d)) – 2010-06-07 12:44:59

0

謝謝你的回覆!

現在它完美的工作。

我正要問另一個問題,但我想通了。

Output.Text = CStr的((A *原(0))/期望的(d))

必須被轉換到

Output.Text = CStr的(( a * original(o-1))/ desired(d-1))

Euxaristw!