首页 > 解决方案 > VB.net 将一维数组拆分为二维数组

问题描述

我目前正在使用 VB.net 编写一个(多项选择)测验程序我已将 .txt 文件中的答案选项读入一维临时数组,但是我想将其分解为每个问题和每个问题的答案选项问题转换为二维数组,因此 (0,0) 将成为问题一的选项 A,然后 (0,1) 将成为问题 1 的选项 B 等,然后 (1,0) 将成为问题 2 的选项 A。下面是我尝试过的方法,但是当我尝试运行这部分代码时,将 1 添加到变量时出现错误:“对象引用未设置为对象的实例”optnum = optnum + 1任何帮助将不胜感激,无论是修复我的下面的代码或建议另一种方法。

    Dim optnum As Integer
    Dim tempq As Integer = 0
    Gameload(Phase_3.sounds)
    Questionnum = 0
    L_start.Hide()
    optnum = 0
    'splits the temp array down into options for each question
    For i = 0 To 39

        questions(tempq, optnum) = temparray(i)
        optnum = optnum + 1
        'there are 4 options for each question
        'moves on to the next question when there is 4 options in the question
        If optnum = 3 Then
            tempq = tempq + 1
            optnum = 0
        End If
    Next

    For i = 0 To 3
        L_option1.Text = questions(0, i)
    Next
    question_set()

编辑:这是新的完整代码我仍然收到错误:对象引用未设置为对象的实例,但现在位于本节代码的下一个。'''
For optnum = 0 To 3 questions(i, optnum) = temparray(i * 4 + optnum) Next ''' 谢谢你到目前为止的所有帮助

'''公开课游戏

Dim submission As Integer
Dim Correct_ans As Integer
Dim temparray() As String
Dim questions(,) As String
Dim Questionnum As Integer
Dim rs As New Resizer
Private Sub Game_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'finds all components on the screen in preperation if the screen resizes
    rs.FindAllControls(Me)
    L_start.Show()
End Sub
Private Sub Game_Resize(sender As Object, e As EventArgs) Handles Me.Resize
    'resizes all components on the screen to same proportions
    rs.ResizeAllControls(Me)
End Sub

Sub Gameload(ByVal sounds As String)

    Dim pack As String

    'reads in the sound pack
    pack = My.Resources.ResourceManager.GetString(sounds)
    Phase_3.Close()
    'splits the pack into an array so that it can be broken down into questions
    temparray = pack.Split(","c)


End Sub

Sub L_start_Click(sender As Object, e As EventArgs) Handles L_start.Click
    Dim optnum As Integer
    Dim tempq As Integer = 0
    Gameload(Phase_3.sounds)
    Questionnum = 0
    L_start.Hide()
    optnum = 0
    'splits the temp array down into options for each question
    For i = 0 To temparray.Count / 4
        For optnum = 0 To 3
            questions(i, optnum) = temparray(i * 4 + optnum)
        Next
    Next

    For i = 0 To 3
        L_option1.Text = questions(0, i)
    Next

结束子

'''

标签: arraysvb.net

解决方案


这只是我的意见,但我认为你的方法很难维持。在我真正尝试过一个课程之前,我从未看到过课程的价值。也许这是你的机会。

Public Class Quiz
    Public Property QuestionNumber As Integer
    Public Property Question As String
    Public Property AnswerA As String
    Public Property AnswerB As String
    Public Property AnswerC As String
    Public Property AnswerD As String
    Public Property CorrectAnswer As String

    Public Overrides Function ToString() As String
        Return $"Question: {QuestionNumber}.{Question} Answer Choices: {AnswerA}, {AnswerB}, {AnswerC}, {AnswerD} Correct Answer - {CorrectAnswer}"
    End Function
End Class

要使用您的课程...

Private QuestionList As New List(Of Quiz)

Private Sub OPCode2()
    Dim temparray = File.ReadAllLines("answers.txt")
    Dim temparraylocation As Integer
    Dim questions = File.ReadAllLines("questions.txt")
    Dim correctAnswers = File.ReadAllLines("correct.txt")
    For i = 0 To questions.Length - 1
        Dim qu As New Quiz
        qu.QuestionNumber = i + 1
        qu.Question = questions(i)
        qu.CorrectAnswer = correctAnswers(i)
        qu.AnswerA = temparray(temparraylocation)
        temparraylocation += 1
        qu.AnswerB = temparray(temparraylocation)
        temparraylocation += 1
        qu.AnswerC = temparray(temparraylocation)
        temparraylocation += 1
        qu.AnswerD = temparray(temparraylocation)
        temparraylocation += 1
        QuestionList.Add(qu)
    Next
    For Each q In QuestionList
        Debug.Print(q.ToString)
    Next
End Sub

推荐阅读