投稿者 魔界の仮面弁士  (社会人) 投稿日時 2022/12/27 15:40:23
> 初級講座19 静的配列にある電車の所要時間での質問です。
VB6/VBA 版の方のコレですね?
http://rucio.o.oo7.jp/main/shokyu/jugyou19.htm

> 下り方向ではどうのように選んでも0分になります。
おそらく、上り/下りの判定処理が不足しているのではないでしょうか。
「初級講座24 デバッグ」の『ステップ実行』を習得すると、
何が問題なのかを追跡しやすくなるかと思います。


> 改訂版を出してください。
るきおさんに代わって、少し書き換えてみました。
ここでは、動的配列の作成に Split 関数や Array 関数を使っています。

Dim Stations() As String
Stations = Split("津田沼 東船橋 船橋 西船橋 下総中山 本八幡 市川")
Dim TrainTimes() As Variant
TrainTimes = Array(3, 2, 3, 2, 4, 4)

Dim StartStation As Integer, EndStation As Integer

''東船橋から本八幡まで 
'StartStation = 1 
'EndStation = 5 

'市川から津田沼まで 
StartStation = 6
EndStation = 0

Dim idx As Integer, Jikan As Integer
Jikan = 0
If StartStation <= EndStation Then
    For idx = StartStation To EndStation - 1
        Jikan = Jikan + TrainTimes(idx)
    Next
Else
    For idx = EndStation To StartStation - 1
        Jikan = Jikan + TrainTimes(idx)
    Next
End If
Dim msg As String
msg = Stations(StartStation) & "から" & Stations(EndStation) & "までは" & CStr(Jikan) & "分です"
MsgBox msg, vbInformation