读取之前未读过的随机excel单元格

时间:2016-12-03 19:14:19

标签: excel vb.net

我的程序为用户提供基于excel文件的测验。问题以随机顺序出现。 excel文件的每一行有5个可能的问题(n,3-7),答案总是在该行的第二个单元格中(n,2)。有135行,但前两行与问题无关。用户得到正确回答问题的点,并且他们应该尝试在时间限制内回答尽可能多的问题,因此当时间到了时,用户将永远不会看到未提出的问题。我需要帮助的问题是,有一个难得的机会(665中有1个)可以重复一个问题。我怎么能阻止这个? (另外,我对编程很新)

问题代码

Private Sub newquestion()
    'New Question
    Randomize()
    row = CInt(rnd.Next(3, 135))
    key = CInt(rnd.Next(3, 7))
    lblgametype.Text = "Guess the answer from the hint"
    lblquestion.Text = worksheet.Cells(row, key).Value
End Sub

检查答案的代码

Private Sub OnKeyDownHandler(ByVal sender As Object, ByVal e As KeyEventArgs) Handles txtanswer.KeyDown
    'Prevent that annoying ass windows ding sound
    If e.KeyCode = Keys.Enter Then
        e.SuppressKeyPress = True
    End If
    If e.KeyCode = Keys.Escape Then                
        e.SuppressKeyPress = True                  
    End If                                         

    If e.KeyCode = Keys.Enter Then 'If the user presses Enter while txtanswer is selected...
        userguess = txtanswer.Text 'Assign the text the user enters to the global userguess variable

        If userguess.ToUpper = worksheet.Cells(row, 2).Value.ToString.ToUpper Then
            'User answers a question correct
            lblcorrect.ForeColor = Color.Green       
            lblcorrect.Text = "Correct. +1"          
            txtanswer.Text = ""                      
            userguess = ""                           
            abilityscore += 1                        
            lblscore.Text = "Score: " & abilityscore 
            If abilityscore > abilityhighscore Then  
                abilityhighscore = abilityscore      
            End If                                   

            newquestion()

        Else
            'User answers a question incorrectly
            lblcorrect.ForeColor = Color.Red       
            lblcorrect.Text = "incorrect."         
            txtanswer.Text = ""
        End If
    End If
    If e.KeyCode = Keys.Escape Then 'If the user presses escape while txtanswer is selected...
        btnskip.PerformClick() 'Treat it as if they pressed skip
    End If
End Sub

问题跳过的代码

Private Sub btnskip_Click(sender As Object, e As EventArgs) Handles btnskip.Click
    Me.TargetDT = Me.TargetDT.Subtract(TimeSpan.FromSeconds(15)) 'Subtract 15 seconds from the timer
    txtanswer.Focus() 'Reset focus back to the textbox

    newquestion()
End Sub

1 个答案:

答案 0 :(得分:1)

132行,每行有5个候选问题,是一种奇怪的存储方式。我猜测它基本上是5种方式来提出相同的问题,或者至少它们具有相同的正确答案。

我无法确定,但似乎确保每次选择不同的行就足够了。

Public Class frmQuizzer

    ' form level variables
    Private RNG As New Random()
    Private rowNums As Int32()
    Private rowIndex As Int32

我猜一场比赛或一轮将是10个问题。因此,在NewGame方法中(这样您可以在不重新启动应用程序的情况下再次运行它):

' which row to use this turn
rowIndex = 0
rowNums = Enumerable.Range(3, 135).
            OrderBy(Function(r) RNG.Next()).
            Take(10).
            ToArray()

这是你要使用的10个不同的XL行。您也可以使用RNG对象选择单元格:

key = RNG.Next(3,8)         ' the MAX is exclusive!
row = rowNums(rowIndex)
' "move" to next question
rowIndex += 1
lblquestion.Text = worksheet.Cells(row, key).Value

就个人而言,我可能会使用一个rowIndex而不是跟踪一个可能搞砸的Stack,而Private rowNums As Stack(Of Int32) 就像一个套牌鞋一样,使用"向上"行号:

Dim nums = Enumerable.Range(3, 135).
            OrderBy(Function(r) RNG.Next).
            Take(10).
            ToArray()
rowNums = New Stack(Of Int32)(nums)

从创建的数组中填写:

' Pop gets the next value and removes it from the list
lblquestion.Text = worksheet.Cells(rowNums.Pop(), key).Value

获取并使用它:

<li>

没有索引器跟踪,也没有机会失去同步。

更多