2015-07-02 60 views
0

我有一個任務,我們必須創建一個項目,查找兩個城市之間的駕駛距離。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 

回答

1

這很好,你注意到For循環的重複和想法,但你實際上並不需要這個問題。

你正在做的當量:

  • 如果x = 0且y = 0,則看陣列(0,0)
  • 如果x = 0且y = 1,則看陣列(0,1)
  • ...
  • 如果x = 9和y = 9然後看陣列(9,9)

其可以表示更簡單地稱爲:

  • 看看陣列(X,Y)

因此,對於你真正的代碼,它是像這樣簡單:

DistanceLbl.Text = (Distance(
    ComboBox1.SelectedIndex, 
    ComboBox2.SelectedIndex).ToString & " miles") 
+0

非常感謝球員。 – Jay

2

直接使用的SelectedIndex膠印對您數組:

Distance(ComboBox1.SelectedIndex, ComboBox2.SelectedIndex) 

你可能會需要的情況下的測試「無選擇」,其中的SelectedIndex爲-1,試圖訪問,在偏移數組將得到一個ArgumentOutOfRangeException。

0

它看起來對我來說,你只需要使用的值訪問數組時從組合框中移除。這可以代替整個If..Elseif部分代碼。

DistanceLbl.Text = (Distance(ComboBox1.SelectedIndex, ComboBox2.SelectedIndex).ToString & " miles")