0

我正在为 Classic ASP 中的客户制作一个简单的问卷。

这个想法是会有10个问题。用户注册并被发送到第一个问题。回答完毕后,他们将继续进行第二个问题,依此类推。可以跳过问题并在以后返回,每个问题只能回答一次。

我在用户回答的每个问题的数据库中都有一个逗号分隔的列表。

因此,用户登录并创建一个包含已回答问题列表的数组。

遍历此列表并转到第一个未回答的问题的最佳方法是什么?

已回答问题数组的示例类似于“1,4,6”,因此该用户将回答第 1、4 和 6 号问题。当用户登录时,我想将他们引导至第一个未回答的问题问题,在这种情况下为 2。一旦第二个问题得到回答,用户将被重定向到下一个未回答的问题。

请问有什么建议吗?

4

1 回答 1

0

@Dog,我认为这提供了您正在寻找的功能。

提示:有关将 Microsoft 的权威 WSH 参考下载为 Windows 帮助文件的信息,请参阅此答案。

Option Explicit

Dim oQsm : Set oQsm = New QuestionStatusManager

With oQsm
    .NumberOfQuestions = 10
    .RestoreStatus("1,4,6")
    .MarkQuestionAnswered(2)
    WScript.Echo "Questions " & .ToString() & " have been answered."
    WScript.Echo "Next unanswered question is: " & .GetNextUnansweredQuestion()
End With

Set oQsm = Nothing

' ------------------------------------------------------------------------

Class QuestionStatusManager

Private m_nNumberOfQuestions
Private m_aQuestionList()

Sub Class_Initialize()
    m_nNumberOfQuestions = -1
End Sub

Sub Class_Terminate()
    Erase m_aQuestionList
End Sub

Public Property Let NumberOfQuestions(n)
    Dim bValid : bValid = False

    If IsNumeric(n) Then
        If n = CInt(n) Then
            bValid = True
        End If
    End If

    If Not bValid Then
        Err.Raise vbObjectError + 1, "", _
           "Value '" & n & "' is not an integer."
    End If

    m_nNumberOfQuestions = CInt(n)
    ReDim m_aQuestionList(n)
End Property

Public Property Get NumberOfQuestions()
    CheckState
    NumberOfQuestions = m_nNumberOfQuestions
End Property

Private Sub CheckState()
    If m_nNumberOfQuestions = -1 Then
        Err.Raise vbObjectError + 1, "", _
           "Property 'NumberOfQuestions' has not been set."
    End If
End Sub

Sub RestoreStatus(sAlreadyAnswered)
    CheckState

    Dim aAlreadyAnswered : aAlreadyAnswered = Split(sAlreadyAnswered, ",")
    Dim i

    For i = 0 To UBound(m_aQuestionList)
        m_aQuestionList(i) = False
    Next

    For i = 0 To UBound(aAlreadyAnswered)
        m_aQuestionList(CInt(aAlreadyAnswered(i))) = True
    Next
End Sub

Sub MarkQuestionAnswered(n)
    Dim sDesc

    CheckState

    On Error Resume Next
    m_aQuestionList(n) = True

    If Err Or n = 0 Then
        sDesc = Err.Description
        On Error GoTo 0
        Err.Raise vbObjectError + 1, "", _
           "Can't mark question number '" & n & "' as answered: " & sDesc
    End If
End Sub

Function GetNextUnansweredQuestion()
    CheckState

    Dim i

    For i = 1 To UBound(m_aQuestionList)
        If Not m_aQuestionList(i) Then
            GetNextUnansweredQuestion = i
            Exit Function
        End If
    Next

    GetNextUnansweredQuestion = -1
End Function

Function ToString()
    CheckState

    Dim sDelim : sDelim = ""
    Dim i

    ToString = ""
    For i = 1 To UBound(m_aQuestionList)
        If m_aQuestionList(i) Then
            ToString = ToString & sDelim & CStr(i)
            sDelim = ","
        End If
    Next
End Function

End Class
于 2012-08-22T01:47:34.130 回答