1 / 13

Array - 陣列

Array - 陣列. Array – 陣列. 一連串同樣型態的變數 Eg: 身高陣列 Dim a() as Integer a(0)=173, a(1)=159, a(2)=180 …. Example. ' 宣告 Temperature 為整數陣列,並且初始化 Temperature 的值 ' 在這裡我們填入了 12 個值,代表每個月份的平均溫度 Dim Temperature() As Integer = {15, 17, 20, 23, 26, 30, 35, 35, 33, 32, 28, 25}

booth
Download Presentation

Array - 陣列

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Array - 陣列

  2. Array –陣列 • 一連串同樣型態的變數 • Eg: 身高陣列 Dim a() as Integer • a(0)=173, a(1)=159, a(2)=180 …

  3. Example '宣告Temperature為整數陣列,並且初始化Temperature的值 '在這裡我們填入了12個值,代表每個月份的平均溫度 Dim Temperature() As Integer = {15, 17, 20, 23, 26, 30, 35, 35, 33, 32, 28, 25} '宣告變數sum,用來計算12個月份的平均溫度 Dim sum As Double '利用ForEach迴圈來加總每個月份的平均溫度 For Each con As Integer In Temperature sum += con Next sum /= 12 TextBox1.Text = sum 建立了一個陣列 可以用For Each迴圈,列舉出所有陣列的值 sum=sum/12

  4. 陣列的宣告

  5. Dimension (陣列的維度)

  6. 陣列機制 執行結果如下:

  7. 動態變更陣列長度

  8. Example Sub main() dim username() ={“Eric”, “Mary”, “Amy”, “Lisa”, “Alax”} for i as integer =0 to UBound(username) Console.writeline(“陣列中第”& I & “個元素值為:” & username(i)) next Console.readkey() End sub

  9. For Each …Next For Each element [As type] In Array [statement] Next • eg: for each score as single in scores

  10. Practice (練習) • 建立一個程式,讓使用者輸入班上所有學員的考試成績(0-100),並且計算出平均分數,最後列出考試成績優於平均分數的學生。 • 提示:利用陣列機制

  11. Practice (練習) • 請建立一段密碼判斷的程式,利用Console.ReadLine要求使用者輸入密碼(密碼可事先以字串定義好,例如Dim Pwd As String=”12345”),以Do…Loop迴圈判斷使用者輸入的密碼是否正確,若密碼錯誤,則要求使用者重新輸入,直到使用者輸入正確的密碼為止。 • 若密碼正確,則跳離Do…Loop迴圈,顯示出『歡迎進入系統』。

More Related