我有一個任務,我們必須創建一個項目,查找兩個城市之間的駕駛距離。Visual Basic中的數組(與處理駕駛距離的程序相關)
使用兩個包含城市名稱的下拉列表。標出一張清單「出發」和另一張「目的地」。使用查找按鈕來計算距離。將距離存儲在二維表中。
我開始這樣做,但我注意到我將不得不重複代碼太多次,我知道有更好的方法來做到這一點。這是我需要幫助的地方。
有沒有辦法在這個問題中使用For循環,如果是這樣,我該怎麼做?
Option Strict On
Public Class Form1
Dim Distance(,) As Long = {{0, 1004, 1753, 2752, 3017, 1520, 1507, 609, 3155, 448},
{1004, 0, 921, 1780, 2048, 1397, 919, 515, 2176, 709},
{1753, 921, 0, 1230, 1399, 1343, 517, 1435, 2234, 1307},
{2752, 1780, 1230, 0, 272, 2570, 1732, 2251, 1322, 2420},
{3017, 2048, 1399, 272, 0, 2716, 1858, 2523, 1278, 2646},
{1520, 1397, 1343, 2570, 2716, 0, 860, 1494, 3447, 1057},
{1507, 919, 517, 1732, 1858, 860, 0, 1307, 2734, 1099},
{609, 515, 1435, 2251, 2523, 1494, 1307, 0, 2820, 571},
{3155, 2176, 2234, 1322, 1278, 3447, 2734, 2820, 0, 2887},
{448, 709, 1307, 2420, 2646, 1057, 1099, 571, 2887, 0}}
Private Sub LookUpBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LookUpBtn.Click
If ComboBox1.SelectedIndex = 0 And ComboBox2.SelectedIndex = 0 Then
DistanceLbl.Text = (Distance(0, 0).ToString & " miles")
ElseIf ComboBox1.SelectedIndex = 0 And ComboBox2.SelectedIndex = 1 Then
DistanceLbl.Text = (Distance(0, 1).ToString & " miles")
ElseIf ComboBox1.SelectedIndex = 0 And ComboBox2.SelectedIndex = 2 Then
DistanceLbl.Text = (Distance(0, 2).ToString & " miles")
ElseIf ComboBox1.SelectedIndex = 0 And ComboBox2.SelectedIndex = 3 Then
DistanceLbl.Text = (Distance(0, 3).ToString & " miles")
ElseIf ComboBox1.SelectedIndex = 0 And ComboBox2.SelectedIndex = 4 Then
DistanceLbl.Text = (Distance(0, 4).ToString & " miles")
ElseIf ComboBox1.SelectedIndex = 0 And ComboBox2.SelectedIndex = 5 Then
DistanceLbl.Text = (Distance(0, 5).ToString & " miles")
ElseIf ComboBox1.SelectedIndex = 0 And ComboBox2.SelectedIndex = 6 Then
DistanceLbl.Text = (Distance(0, 6).ToString & " miles")
ElseIf ComboBox1.SelectedIndex = 0 And ComboBox2.SelectedIndex = 7 Then
DistanceLbl.Text = (Distance(0, 7).ToString & " miles")
ElseIf ComboBox1.SelectedIndex = 0 And ComboBox2.SelectedIndex = 8 Then
DistanceLbl.Text = (Distance(0, 8).ToString & " miles")
ElseIf ComboBox1.SelectedIndex = 0 And ComboBox2.SelectedIndex = 9 Then
DistanceLbl.Text = (Distance(0, 9).ToString & " miles")
End If
End Sub
End Class
非常感謝球員。 – Jay