2016-12-03 52 views
0

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

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 
+2

的问题是随机的,并不意味着唯一的。你需要一个shuffle:见[选择唯一的随机数字](http:// stackoverflow。com/q/35120454/1070452) – Plutonix

+0

@Plutonix我读过你对这个家伙问题的回答,我相信我要找的答案是在答案的“随机对”部分。我从来没有使用过列表或数组。你可以告诉我,我的代码会是什么样子?另外,感谢您的帮助。 – Purin

+0

如果你的测验有5个问题:'rowNums = Enumerable.Range(3,135).OrderBy(Function(r)RNG.Next())。拿(5).ToArray()'来保存所有5个x1行号为这个游戏。这件东西就像一个集中型游戏。 'RNG'将是一个NET的'Random'对象,而不是传统的VB'Rnd'函数 – Plutonix

回答

1

代码每行有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 

没有索引器要跟踪,也没有机会脱离同步。

更多

+0

我现在明白了。谢谢 – Purin