【问题标题】:Selecting a random string from a delimited text file从分隔的文本文件中选择随机字符串
【发布时间】:2014-03-27 20:37:13
【问题描述】:

我正在尝试将所有已解析的字符串输入到一个二维数组中,其中前 5 个字符串组成一列,然后接下来的 5 个组成第二列等。然后我需要随机选择 3 列并输出该列中的每个字符串。

        path = My.Computer.FileSystem.GetFileInfo("Two_Point_One_Questions.txt")
    Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(path.FullName)
        MyReader.TextFieldType = FileIO.FieldType.Delimited
        MyReader.SetDelimiters(",")
        Dim currentRow As String()
        While Not MyReader.EndOfData
            Try
                currentRow = MyReader.ReadFields()
                Dim currentField As String
                Dim i As Integer = 1
                Dim j As Integer = 1
                For Each currentField In currentRow
                    Question(i, j) = currentField
                    j = j + 1
                Next
            Catch ex As Microsoft.VisualBasic.
                        FileIO.MalformedLineException
                MsgBox("Line " & ex.Message &
                "is not valid and will be skipped.")
            End Try
        End While
    End Using

【问题讨论】:

  • 问题是……?
  • 你不要增加变量 i

标签: arrays vb.net vba random


【解决方案1】:

我不确定您要做什么,但您需要增加变量 i 以填充 Question 数组上的新槽

        ....
        End Try
        i = i + 1
    End While
End Using

顺便说一句,数组从索引零开始,而不是从一开始。所以可能你对 i, y 的陈述值是错误的

Dim i As Integer = 0
Dim j As Integer = 0

最后,变量 i 应该在进入 while 循环之前被声明和初始化,否则它将被重置为其初始值,并且从文件中读取的行将始终进入数组的同一槽

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    相关资源
    最近更新 更多