2016-12-03 83 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