Presentation is loading. Please wait.

Presentation is loading. Please wait.

情報基礎A 第11週 プログラミング入門 VBAの基本文法3 配列・For~Next

Similar presentations


Presentation on theme: "情報基礎A 第11週 プログラミング入門 VBAの基本文法3 配列・For~Next"— Presentation transcript:

1 情報基礎A 第11週 プログラミング入門 VBAの基本文法3 配列・For~Next
徳山 豪・全 眞嬉 東北大学情報科学研究科 システム情報科学専攻 情報システム評価学分野

2 配列 同じデータ型は配列としてまとめて扱う 大量のデータを扱う時や複数のデータを次々と自動的に読み出したい時に配列を利用する
水色の箱は整数の箱 (Integer型) x2 x5 x3 x4 x1 x6 x(1) x(2) x(3) x(4) x(5) x(6) インデックス番号 要素 箱をx(1),x(2)・・・ 配列の名前

3 配列の宣言(インデックス番号指定) Dim x (1 to 6) As Integer 整数型変数を入れる6つの箱
要素 箱をx(1),x(2)・・・, x(6) 配列の名前 Dim x (1 to 6) As Integer データ型 配列名 インデックス番号の範囲

4 配列の宣言(一般的な方法) Dim x (5) As Integer インデックス番号は「0」から始まる 整数型変数を入れる6つの箱
x(5):名前がxの整数型の箱を6個用意 Dim x (5) As Integer  データ型 配列名 インデックス番号 の最大値

5 配列の宣言(一般的な方法) 学籍番号1番 100点 学籍番号3番 76点 学籍番号5番 61点 学籍番号2番 65点 学籍番号4番 87点
学籍番号6番 99点 socre(0) socre(1) socre(2) socre(3) socre(4) socre(5) score(0) = 100 score(1) = 65 score(2) = 76 score(3) = 87 score(4) = 61 score(5) = 99

6 配列の宣言 Sub hairetsu1() Dim score(5) As Integer score(0) = 100
Msgbox score(1) End Sub     

7 Sub hairetsu2() Dim score(5) As Integer Dim number As Integer score(0) = 100 score(1) = 65 score(2) = 76 score(3) = 87 score(4) = 61 score(5) = 99   number = InputBox(“学籍番号1番から6番までの6人分の成績を参照できます.知りたい学籍番号を教えてください”) Msgbox  “学生番号” & number  & “番の成績は”   & score(number - 1)  & “点です.” End Sub     

8 For~ Next i<=5 繰り返す回数を指定して処理を行う 繰り返し回数:6回 For i = 0 To 5 Step 1
カウンタという変数を用意し,その範囲を指定し,繰り返す回数を決める カウンタ変数名 : i 繰り返し回数:6回 iの値を1つ増やすi+1をiに代入 i : カウンタ変数名 i=0 カウンタの初期値 カウンタの最大値 For  i  = 0 To 5 Step 1 Next i 動作1 i=i+1 カウンタの増分設定 i<=5 true 動作1 false

9 カウンタ i Dim i As Integer i=0 i = i +1 の意味 i+1 の値を i に代入する iの値を1つ増やす 0+1
Dim i As Integer i=0 カウンタ名は 自分で決める カウンタ名 : i i = i +1 の意味  i+1 の値を i に代入する iの値を1つ増やす 0+1 i i + 1

10 i 2 3 1 i = i +1 0+1 i i + 1 i = i +1 1+1 i i + 1 i = i +1 2+1 i + 1 i

11 Sub hairetsu3() ‘For ~ Next を使い6人分の成績を順番に表示(6回繰り返す) ‘配列名score カウンタ名:number Dim score(5) As Integer Dim number As Integer score(0) = 100 score(1) = 65 score(2) = 76 score(3) = 87 score(4) = 61 score(5) = 99 For number = 0 To 5 Step 1    Msgbox  “学生番号” & number +1  & “番の成績は”   & score(number)  & “点です.” Next number End Sub     

12 Sub hairetsu4() Dim score(5) As Integer Dim name(5) As String
‘For ~ Next を使い6人分の学籍番号,名前,成績を ‘順番に表示(6回繰り返す) ‘点数の配列名score 名前の配列名name1 カウンタ名:number Dim score(5) As Integer Dim name(5) As String Dim number As Integer score(0) = 100 score(1) = 65 score(2) = 76 score(3) = 87 score(4) = 61 score(5) = 99

13 name1(0)=“田中浩二”   name1(1)=“阿部弘”   name1(2)=“伊藤明子”   name1(3)=“鈴木一郎”   name1(4)=“加藤貴子”   name1(5)=“木村潤平” For number = 0 To 5 Step 1    Msgbox  “学生番号” & number +1  & “番” & name1(number) & “さんの成績は”   & score(number)  & “点です.” Next number End Sub     

14 演習1 学籍番号,名前,成績,評価を出力するプログラムを作成して下さい 例:学籍番号1番田中浩二さんの点数は100点で秀です.
評価は90点以上なら“秀”、80点以上90点未満は“優”、70点以上80点未満は“良”、60点以上70点未満は“可”, 60点未満は“不可” プログラム名は hyouka() ヒント hairetsu4()の For~Next 文とseiseki2()の If ~Then ~Else 文を使う

15 6人分の合計を計算 sum i<=5 i=0, sum=0 i=0 の時 sum =sum+score(0) i=i+1
socre(0) socre(1) socre(2) socre(3) socre(4) socre(5) sum i=0, sum=0 i=0 の時  sum =sum+score(0) i=i+1 i<=5 sum=sum+score(i)

16 6人分の合計を計算 初期値: sum 100 65 76 87 61 99 i=0 sum sum 1回目 100 100 + i=1
socre(0) socre(1) socre(2) socre(3) socre(4) socre(5) sum i=0 sum score(0) sum 1回目 100 100 + i=1 100 sum score(1) sum 2回目 65 165 +

17 6人分の合計を計算 165 sum score(2) + 76 241 3回目 389 sum score(5) + 99 488 6回目

18 Sub goukei() ‘For ~ Next を使い6人分の成績の合計を計算 ‘配列名score カウンタ名:number  合計の変数:sum Dim score(5) As Integer Dim number As Integer Dim sum As Integer score(0) = 100 score(1) = 65 score(2) = 76 score(3) = 87 score(4) = 61 score(5) = 99 For number = 0 To 5 sum = sum + score(number)   Next number MsgBox number & “人の成績合計点は” & sum &”です.” End Sub     


Download ppt "情報基礎A 第11週 プログラミング入門 VBAの基本文法3 配列・For~Next"

Similar presentations


Ads by Google