Arrays 向数组中添加项(VB 2008)
该程序的目标是使用StreamReader从文件中解释曲棍球统计数据,然后显示添加的分数列。下面的代码可以这样做,但是它没有将points值添加到数组中,这是无效的——它单独输出它。寻求有关如何将points值合并到aryTextFile()中的帮助Arrays 向数组中添加项(VB 2008),arrays,vb.net,Arrays,Vb.net,该程序的目标是使用StreamReader从文件中解释曲棍球统计数据,然后显示添加的分数列。下面的代码可以这样做,但是它没有将points值添加到数组中,这是无效的——它单独输出它。寻求有关如何将points值合并到aryTextFile()中的帮助 Dim hockeyFile、LineOfText、aryTextFile()作为字符串 作为整数的Dim i Dim nameText()、NumberText()、goalsText()、AssistText()、GamesContext()作
Dim hockeyFile、LineOfText、aryTextFile()作为字符串
作为整数的Dim i
Dim nameText()、NumberText()、goalsText()、AssistText()、GamesContext()作为字符串
Dim INTASSITS()、IntGoals()、PointsText()作为单个
hockeyFile=“C:\Users\Bob\Downloads\hockey.txt”'说明文件的位置
Dim objReader作为新的System.IO.StreamReader(hockeyFile)'objReader可以读取hockeyFile
对于objReader.Peek()-1'而言,i=0分别读取每一行,在没有更多数据可读取时结束
LineOfText=objReader.ReadLine'将HockeyFile中的单独数据行存储到LineOfText中
aryTextFile=LineOfText.Split(“,”)获取行并将数据转换为数组
Name=aryTextFile(0)'文本行中的第一条数据是名称
nameText(i)=aryTextFile(0)
如果名称文本(0)=“名称”,则
TextBox1.Text=LineOfText&“,Points.”&vbCrLf'显示文本的第一行并添加“Points”标签
如果结束
若名称为“Name”,则在第二行开始时,开始解释数据
NumberText(i)=aryTextFile(1)
assistext(i)=aryTextFile(2)'辅助位于数组的第三个值中
goalsText(i)=aryTextFile(3)'目标位于数组的第四个值中
GamesContext(i)=aryTextFile(4)
IntAssists(i)=Val(assistext(i))'由于我们的assists值默认为字符串,因此必须将其转换为整数
IntGoals(i)=Val(goalsText(i))'由于我们的目标值默认为字符串,因此必须将其转换为整数
PointsText(i)=(IntGoals(i)*2)+(IntAssists(i))'进球是两分,助攻是一分
TextBox1.Text=TextBox1.Text&NumberText(i)&assistsText(i)&goalsText(i)&GamesWonText(i)&PointsText(i)&vbCrLf'将点显示为每行中的最后一个值
如果结束
接下来我
这应该会让你非常接近:
它需要额外的验证。它没有考虑名字和目标之间的任何价值
Private Sub ProcessHockeyStats()
Try
Dim inputFile As String = "c:\temp\hockey.txt"
Dim outputFile As String = "c:\temp\output.txt"
If Not File.Exists(inputFile) Then
MessageBox.Show("Missing input file")
Return
End If
If File.Exists(outputFile) Then
File.Delete(outputFile)
End If
Dim lines() As String = File.ReadAllLines(inputFile)
Dim output As List(Of String) = New List(Of String)
Dim firstLine As Boolean = True
For Each line As String In lines
Dim values() As String = line.Split(","c)
Dim points As Integer
If firstLine Then
output.Add("Name, Assists, Goals, Points")
firstLine = False
Else
'needs validation for values
points = CInt(values(1) * 2) + CInt(values(2))
output.Add(String.Concat(line, ",", points))
End If
Next
File.WriteAllLines("c:\temp\outfile.txt", output)
Catch ex As Exception
MessageBox.Show(String.Concat("Error occurred: ", ex.Message))
End Try
End Sub
并行阵列并不是解决这个问题的方法。创建一个类或结构来组织数据。然后创建类的列表。该列表可以设置为
DataGridView
的DataSource
,该视图将在漂亮的列中显示数据,标题与曲棍球类中的属性名称匹配。您可以根据曲棍球的任何属性在曲棍球列表中轻松排序数据
Public Class Hockey
Public Property Name As String
Public Property Number As String
Public Property Goals As Integer
Public Property Assists As Integer
Public Property Points As Integer
Public Property GamesWon As Integer
End Class
Private HockeyList As New List(Of Hockey)
Private Sub FillListAndDisplay()
Dim path = "C:\Users\Bob\Downloads\hockey.txt"
Dim Lines() = File.ReadAllLines(path)
For Each line As String In Lines
Dim arr() = line.Split(","c)
Dim h As New Hockey()
h.Name = arr(0)
h.Number = arr(1)
h.Assists = CInt(arr(2).Trim)
h.Goals = CInt(arr(3).Trim)
h.GamesWon = CInt(arr(4).Trim)
h.Points = h.Goals * 2 + h.Assists
HockeyList.Add(h)
Next
Dim orderedList = (From scorer In HockeyList Order By scorer.Points Ascending Select scorer).ToList
DataGridView1.DataSource = orderedList
End Sub
VS2008是古老的,尤其是在Visual Studio的更高版本是免费的时候。我想用更新的代码展示一个实现。与其他人一样,我强烈支持为此构建一个类。不同之处在于,我的类更聪明一些,使用工厂模式创建实例,并根据需要使用属性计算
点
:
Public Class HockeyPlayer
Public Property Name As String
Public Property Number As String
Public Property Assists As Integer
Public Property Goals As Integer
Public Property Wins As Integer
Public ReadOnly Property Points As Integer
Get
Return (Goals * 2) + Assists
End Get
End Property
Public Shared Function FromCSVLine(line As String) As HockeyPlayer
Dim parts() As String = line.Split(",")
Return New HockeyPlayer With {
.Name = parts(0),
.Number = parts(1),
.Assists = CInt(parts(2)),
.Goals = CInt(parts(3)),
.Wins = CInt(parts(4))
}
End Function
End Class
Dim hockeyFile As String = "C:\Users\Bob\Downloads\hockey.txt"
Dim players = File.ReadLines(hockeyFile).Skip(1).
Select(Function(line) HockeyPlayer.FromCSVLine(line)).
ToList() 'ToList() is optional, but I included it since you asked about an array
Dim result As New StringBuilder("Name, Number, Assists, Goals, Wins, Points")
For Each player In players
result.AppendLine($"{player.Name}, {player.Number}, {player.Assists}, {player.Goals}, {player.Wins}, {player.Points}")
Next player
TextBox1.Text = result.ToString()
我本来打算在接下来给你VS2008版本,但是看看这个,这里你唯一不能做的事情,即使VS2010是字符串插值。。。您真的应该升级。您能在输入文件中显示数据行的示例吗?谢谢您的时间。我有一个简短的问题-我注意到您将输出声明为列表。是否有任何方法可以直接将我已经拥有的数组转换为列表?Dim testNewList As list(Of String)=New list(Of String)testNewList.AddRange(lines)Catch ex As Exception代码段做什么?它用于错误处理:我正在做的任务需要使用VB 2008。我们还没有被介绍去上课;这个特定的任务需要使用StreamReader/Writer和阵列,因此我发现它非常具有挑战性。有没有办法让我发布的代码生效?
Public Class HockeyPlayer
Public Property Name As String
Public Property Number As String
Public Property Assists As Integer
Public Property Goals As Integer
Public Property Wins As Integer
Public ReadOnly Property Points As Integer
Get
Return (Goals * 2) + Assists
End Get
End Property
Public Shared Function FromCSVLine(line As String) As HockeyPlayer
Dim parts() As String = line.Split(",")
Return New HockeyPlayer With {
.Name = parts(0),
.Number = parts(1),
.Assists = CInt(parts(2)),
.Goals = CInt(parts(3)),
.Wins = CInt(parts(4))
}
End Function
End Class
Dim hockeyFile As String = "C:\Users\Bob\Downloads\hockey.txt"
Dim players = File.ReadLines(hockeyFile).Skip(1).
Select(Function(line) HockeyPlayer.FromCSVLine(line)).
ToList() 'ToList() is optional, but I included it since you asked about an array
Dim result As New StringBuilder("Name, Number, Assists, Goals, Wins, Points")
For Each player In players
result.AppendLine($"{player.Name}, {player.Number}, {player.Assists}, {player.Goals}, {player.Wins}, {player.Points}")
Next player
TextBox1.Text = result.ToString()