调查答案在数组中计算

时间:2014-10-14 16:27:08

标签: vb.net-2010

我统计有多少学生回答调查问题1到问题17。

  • Q1至Q15是相似问题(非常不同意/不同意/同意/非常同意
  • Q16是是/否问题:Do you go to the lab?
  • 如果学生回答(= 2),那么Q17就是相似问题。如果Q16 (= 1)则跳过Q17

我可以从Q1到Q15计算,如下面的代码所示,但我无法处理Q16(y / n)。输出需要显示Q1-Q15,跳过Q16并显示Q17。 我还有一张表tblSurveyQuestion(questID, questType, question)。在Q16,questID = Survey16,questType =“Y”。

您能告诉我如何插入代码来处理Q16吗?到目前为止我的代码:

    'assume dt is a datatable that is declared and the connection works
    Dim ans(16, 3) As Integer      '17 questions with Q16 is a y/n question  
    For I As Integer = 0 To dt.Rows.Count - 1
        If dt.Rows(I).Item(2) = 1 Then
            Dim Answer1() As String = dt.Rows(I).Item(3).Split(";")  
            For j1 As Integer = 0 To 16     
                Dim k1 As Integer = (Val(Answer1(j1)))
                ans(j1, k1 - 1) += 1
            Next
        End If
    Next

1 个答案:

答案 0 :(得分:0)

由于您按顺序评估答案,并且您希望在某个时刻了解以前的数据,因此您需要在代码中知道您正在评估(或显示)当前为了采取相应行动的问题。

您已经在ans(15)中对问题16进行了评估,但您只需要检查您是否在索引16(=问题17),并处理ans(15)的结果。一种可能性是使用:

你需要:

  • 声明一个变量来存储Question16的评估结果
  • 当评估问题17检查Question16的存储结果时
  • 相应地采取行动(例如不保存问题17或不显示问题)

一个非常简单的解决方案可能如下所示:

'assume dt is a datatable that is declared and the connection works
Dim ans(16, 3) As Integer      '17 questions with Q16 is a y/n question  
For I As Integer = 0 To dt.Rows.Count - 1
    If dt.Rows(I).Item(2) = 1 Then
        Dim Answer1() As String = dt.Rows(I).Item(3).Split(";")
        'declare a store for the answer of question 16
        Dim answer16IsPositive as Boolean = False
        For j1 As Integer = 0 To 16
            Dim k1 As Integer = (Val(Answer1(j1)))
            ans(j1, k1 - 1) += 1
            If (j1 = 15) ' we are at the 16th answer
                If (k1 = 1) 'save result of answer 16!
                    answer16IsPositive = True
                End If
            End If
            If (j1 = 16) ' we are at the 17th answer
                If (Not answer16IsPositive)
                    ' deal with answer 17!
                'Else
                ' do nothing
                End If
            End If
        Next
    End If
Next