1 / 17

4. Array - 陣列

4. 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}

kelvin
Download Presentation

4. 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. 4. 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. 範例 4-1

  7. 範例 4-2

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

  9. 動態變更陣列長度

  10. 範例 4-3 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

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

  12. 範例4-4 平均溫度 Sub Main() Dim temprature() As Integer = {15, 17, 20, 23, 26, 30, 35, 35, 33, 32, 28, 25} Dim sum, average As Double Dim i As Integer For i = 0 To UBound(temprature) sum = sum + temprature(i) Next average = sum / 12 Console.WriteLine("平均溫度為" & average) Console.ReadKey() End Sub

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

  14. Sub Main() Dim score(4, 1) As String Dim i As Integer Dim sum, avg As Single For i = 0 To 4 Console.Write("請輸入第" & i + 1 & "位同學的名字:") score(i, 0) = Console.ReadLine Console.Write("請輸入第" & i + 1 & "位同學的成績:") score(i, 1) = Console.ReadLine Next For i = 0 To 4 sum = sum + score(i, 1) Next avg = sum / 5 Console.WriteLine("平均為:" & avg) Console.Write("高於平均名單為:") For i = 0 To 4 If score(i, 1) >= avg Then Console.Write(i+1 & “:” & score(i, 0) & " ") End If Next Console.ReadKey() End Sub

  15. 隨堂練習 4-6 精品的價格根據種類有不同的折扣。其計算公式如下: 實售價 = 定價 * 折扣 某日陳董帶著加零小姐來到精品店,精品店共有下列產品: 請撰寫一VB程式,利用函式來計算實售價,並且回答下列問題。 (A) 請問陳董要買下全部精品,需要多少錢? (B) 陳董的預算為200000元,請問哪些”項次”可以購買(實售價小於200000)? (C) 續上題,可購買的品名為何?

  16. Sub Main() Dim item(,) As Single = {{300000, 0.7, 0}, {100000, 0.3, 0}, {200000, 0.7, 0}, {120000, 0.6, 0}, {60000, 0.75, 0}, {180000, 0.2, 0}, {1000000, 0.43, 0}, {500000, 0.62, 0}} Dim name() As String = {"瘦山動物園鱷魚女皮鞋", "LB經典花紋長夾", "PRAD惡魔女洋裝", "古奇時尚保齡球包", "巴巴瑞經典化妝包", "箱奈兒五號香水", "抹茶花滿天星鑚表", "魔戒200克拉精靈美鑚"} Dim i, sum As Single For i = 0 To UBound(item) item(i, 2) = item(i, 0)*item(i, 1) Next For i = 0 To UBound(item) sum = sum + item(i, 2) Next Console.WriteLine("購買所有精品總共需要" & sum & "元") Console.WriteLine("陳董買的起下列精品:") For i = 0 To UBound(item) If item(i, 2) < 200000 Then Console.WriteLine(i + 1 & ". " & name(i)) End If Next Console.ReadKey() End Sub

  17. 隨堂練習 4-7 樂透 • 請撰寫一樂透彩程式,讓使用者輸入五個小於39且不重複的數字當成押注號碼。電腦會自動產生五個不重複的號碼當成開獎結果。比對兩者號碼得出中獎組數,各獲得下面所述之金額。

More Related