MST

星途 面试题库

面试题:Visual Basic中数组与集合处理基础应用

在Visual Basic中,假设有一个包含学生成绩的整数数组`scores(1 to 10)`,要求编写代码找出成绩最高的学生的序号以及对应的成绩,并将结果显示在消息框中。另外,创建一个集合对象,将数组中的成绩大于80分的添加到集合中,最后遍历集合并在立即窗口中输出这些成绩。
48.9万 热度难度
编程语言Visual Basic

知识考点

AI 面试

面试题答案

一键面试
Option Explicit

Sub FindHighestScoreAndCreateCollection()
    Dim scores(1 To 10) As Integer
    Dim i As Integer
    Dim highestScore As Integer
    Dim highestScoreIndex As Integer
    Dim myCollection As New Collection
    '假设给数组赋值,这里只是示例
    scores(1) = 75
    scores(2) = 85
    scores(3) = 90
    scores(4) = 78
    scores(5) = 88
    scores(6) = 95
    scores(7) = 82
    scores(8) = 70
    scores(9) = 80
    scores(10) = 92
    
    highestScore = scores(1)
    highestScoreIndex = 1
    
    For i = 2 To 10
        If scores(i) > highestScore Then
            highestScore = scores(i)
            highestScoreIndex = i
        End If
        If scores(i) > 80 Then
            myCollection.Add scores(i)
        End If
    Next i
    
    MsgBox "成绩最高的学生序号为:" & highestScoreIndex & ",对应的成绩为:" & highestScore
    
    Dim item As Variant
    For Each item In myCollection
        Debug.Print item
    Next item
End Sub