Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays VB.net将一维数组拆分为二维数组_Arrays_Vb.net - Fatal编程技术网

Arrays VB.net将一维数组拆分为二维数组

Arrays VB.net将一维数组拆分为二维数组,arrays,vb.net,Arrays,Vb.net,我目前正在使用VB.net编写一个(多项选择)测验程序。我已将.txt文件中的答案选项读入一个一维临时数组,但我想将其拆分为每个问题,并将每个问题的答案选项拆分为二维数组,以便(0,0)将成为问题1的选项,然后(0,1)问题1的选项B等,然后(1,0)将是问题2的选项A。下面是我尝试过的方法,但是当我尝试运行代码的这一部分时,在向变量添加1时出现错误:“Object reference not set to a instance of a Object”(对象引用未设置为对象的实例) D

我目前正在使用VB.net编写一个(多项选择)测验程序。我已将.txt文件中的答案选项读入一个一维临时数组,但我想将其拆分为每个问题,并将每个问题的答案选项拆分为二维数组,以便(0,0)将成为问题1的选项,然后(0,1)问题1的选项B等,然后(1,0)将是问题2的选项A。下面是我尝试过的方法,但是当我尝试运行代码的这一部分时,在向变量添加1时出现错误:“Object reference not set to a instance of a Object”(对象引用未设置为对象的实例)

    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()
编辑: 下面是新的完整代码,我仍然得到了错误:对象引用没有设置为对象的实例,但现在在代码的下一部分。 ''”
对于optnum=0到3 问题(i,optnum)=临时数组(i*4+optnum) 下一个 ''' 谢谢你迄今为止的帮助

''' 公开课游戏

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
端接头


''

你对临时数组和问题的声明在哪里。测验是否总是有4个选项和10个问题

对您来说,使用嵌套循环可能更容易,因此i为temparray.count/4,optnum为0-3,然后您可以填充问题(i,optnum)=temparray(i*4+optnum)

查看新代码,您还没有初始化questions变量。只是宣布了


就我个人而言,我会把问题改成一个列表,然后使用玛丽的答案

这只是我的意见,但我认为你的建议很难维持。直到我真正尝试了一个类,我才发现它的价值。也许这是你的机会

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

Hi-temparray和问题是全局声明的。它总是有4个选项和10个问题。代码如何查找嵌套循环?鉴于这是一个多么简单的问题,我认为这是一个家庭作业式的问题,因此,我相信您可以通过自己的代码以及我给您的代码来解决此问题。请看一看类似于我认为我已经解决了代码的问题,但是我得到了错误:System.NullReferenceException:'对象引用未设置为对象的实例。'我认为代码为:'',对于I=0到temparray。对于optnum=0到3个问题(i,optnum)=temparray(i*4+optnum)下一个“”,这不是一个家庭作业问题,我只是因为有额外的空闲时间才接触VB.net,我正在尝试做一个小测验。这几乎肯定意味着您的变量尚未初始化。您是否可以编辑您的问题以添加修改后的代码,以及从何处调用它,以及在何处声明和初始化全局变量?谢谢您,这真的很有帮助