0

我有一个来自文本文件的分数数组,我正在尝试遍历它们以将所有分数相加。分数现在在字符串中,所以我需要帮助将它们转换为整数并使用循环将它们全部加起来。

需要转换和相加的分数是p1,p2,p3,p4,p5,p6,exam1,exam2

这是我现在的代码。任何帮助,将不胜感激; 谢谢!

Imports System.IO

Public Class Form1

Dim textFile As String

Private Sub btnSelect_Click(sender As Object, e As EventArgs) Handles btnSelect.Click
    OpenFileDialog1.ShowDialog()
    textFile = OpenFileDialog1.FileName
    Dim data() As String = File.ReadAllLines(textFile)


    Dim myArray As String = data(0)
    Dim studentInfo() = myArray.Split(","c)

    Dim studentname As String = studentInfo(0)
    Dim subject As String = studentInfo(1)
    Dim semester As String = studentInfo(2)
    Dim photo As String = studentInfo(3)
    Dim p1 As String = studentInfo(4)
    Dim p2 As String = studentInfo(5)
    Dim p3 As String = studentInfo(6)
    Dim p4 As String = studentInfo(7)
    Dim p5 As String = studentInfo(8)
    Dim p6 As String = studentInfo(9)
    Dim exam1 As String = studentInfo(10)
    Dim exam2 As String = studentInfo(11)


    lblName.Text = studentname
    lblSubject.Text = subject
    lblSemester.Text = semester
    PictureBox1.Image = Image.FromFile(photo)
    lblP1.Text = p1
    lblP2.Text = p2
    lblP3.Text = p3
    lblP4.Text = p4
    lblP5.Text = p5
    lblP6.Text = p6
    lblExam1.Text = exam1
    lblExam2.Text = exam2
End Sub

End Class
4

1 回答 1

0

我认为您误解了数组是什么。

要创建一个数组:

Dim p as integer(6)

将数据放入数组

p(1) = studentInfo(4)  
p(2) = studentInfo(5)  
'etc  

从数组中获取数据

b = p(1)

遍历数组:

For i as Integer = 1 to 6
    total += p(i)
Next
于 2013-11-15T02:35:29.003 回答