配列・For~Next

Download Report

Transcript 配列・For~Next

情報基礎A 第11週
プログラミング入門
VBAの基本文法3
配列・For~Next
徳山 豪・全 眞嬉
東北大学情報科学研究科
システム情報科学専攻
情報システム評価学分野
1
配列

同じデータ型は配列としてまとめて扱う
– 大量のデータを扱う時や複数のデータを次々と自動
的に読み出したい時に配列を利用する
水色の箱は整数の箱
(Integer型)
x2
x5
x3
x4
x1
x(1)
x(2)
要素
箱をx(1),x(2)・・・
x(3)
x(4)
x(5)
x6
x(6)
インデックス番号
配列の名前
2
配列の宣言(インデックス番号指定)
整数型変数を入れる6つの箱
( 箱の名前はx,箱の番号は1~6 )
x(1)
x(2)
x(3)
x(4)
x(5)
x(6)
インデックス番号
要素
箱をx(1),x(2)・・・, x(6)
Dim x (1 to 6) As Integer
配列名
配列の名前
データ型
インデックス番号の範囲
3
配列の宣言(一般的な方法)
x(1)
x(2)
インデックス
番号は「0」か
ら始まる
x(3)
x(4)
x(5)
整数型変数を入れる6つの箱
( 箱の名前はx ,箱の番号は0~5)
x(0)
x(1)
x(2)
x(3)
x(4)
Dim x (5) As Integer
配列名
x(6)
インデックス番号
の最大値
データ型
x(5)
x(5):名前がx
の整数型の
箱を6個用意
4
配列の宣言(一般的な方法)
学籍番号1番
100点
学籍番号3番
学籍番号5番
76点
61点
学籍番号2番
学籍番号4番
学籍番号6番
65点
87点
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
5
配列の宣言
Sub hairetsu1()
Dim score(5) As Integer
score(0) = 100
score(1) = 65
score(2) = 76
score(3) = 87
score(4) = 61
score(5) = 99
Msgbox score(1)
End Sub
6
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
7
For~ Next

繰り返す回数を指定して処理を行う
– カウンタという変数を用意し,その範囲を指定し,繰り
返す回数を決める
• カウンタ変数名 : i
i=0
繰り返し回数:6回
iの値を1つ増やす
i+1をiに代入
i : カウンタ変数名
カウンタの初期値
カウンタの最大値
i=i+1
i<=5
false
true
動作1
For i = 0 To 5 Step 1
動作1
Next i
カウンタの増分設定8
カウンタ
0
Dim i As Integer
カウンタ名は
i=0
自分で決める
i
カウンタ名 : i
i = i +1 の意味
i+1 の値を i に代入する
iの値を1つ増やす
0+1
i
+ 1
i
9
i = i +1
i
+ 1
i = i +1
i
0312
i
i
1+1
+ 1
i = i +1
i
0+1
i
2+1
+ 1
i
10
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) & “点です.”
11
Next number
Sub hairetsu4()
‘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
12
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
13
演習1

学籍番号,名前,成績,評価を出力するプログラ
ムを作成して下さい
– 例:学籍番号1番田中浩二さんの点数は100点で秀です.
– 評価は90点以上なら“秀”、80点以上90点未満は“優”、
70点以上80点未満は“良”、60点以上70点未満は“可”,
60点未満は“不可”
– プログラム名は hyouka()
– ヒント
• hairetsu4()の For~Next 文とseiseki2()の If ~
Then ~Else 文を使う
14
6人分の合計を計算
socre(0) socre(1) socre(2) socre(3) socre(4) socre(5)
i=0, sum=0
sum
i=0 の時
sum =sum+score(0)
i=i+1
i<=5
sum=sum+score(i)
15
6人分の合計を計算
初期値:
100
0
sum
65
76
87
61
99
socre(0)socre(1)socre(2)socre(3)socre(4)socre(5)
i=0
1回目
0
sum
+
100
sum
+
100
score(0)
100
sum
i=1
2回目
65
score(1)
165
sum
16
6人分の合計を計算
3回目
6回目
165
sum
+
389
sum
+
76
score(2)
99
score(5)
241
sum
488
sum
17
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
18