2016-03-06 2 views
-2

J'ai créé une application Quiz dans Basic.I visuels ont stocké les questions dans un fichier texte et j'utilise streamreader pour lire le fichier texte lines.The ressemble à ceComment afficher aléatoire ques à partir du fichier texte dans vb?

If x is the first of five consecutive odd numbers then what is their average ? 
x 
x+1 
x+4 
x+3 
3 
Which of the following number is divisible by 24 ? 
76300 
78132 
80424 
81234 
3 

La première ligne est la question, les lignes 2 à 5 sont les options et la 6e ligne est la clé de réponse et il y a plus de 100 questions et je devrais imprimer des questions aléatoires et ses choix correspondants chaque fois que j'ouvre l'application et ne répète pas la même question. Quelqu'un peut-il me donner un extrait de code pour cela?

Je recommande
Imports System.IO 
Imports System.Runtime.InteropServices 

Public Class Quiz 
    Public ques As Integer = 1 
    Dim Shuffle As Integer = 0 
    Dim SCORE As Integer = 0 
    Dim val As Integer = 30 
    Public anskey As String 
    Private currentQuestion As Integer 
    Private listOfQuestions As List(Of Question) = New List(Of Question) 
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> 
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr 
    End Function 
    Public Sub Reset_all() 
     val = 30 
     SCORE = 0 
     ProgressBar1.Value = 0 
     Button3.Hide() 
     ProgressBar1.Minimum = 0 
     ProgressBar1.Maximum = 30 
     Timer1.Enabled = True 
     Using reader = New System.IO.StreamReader("Quiz.txt") 
      Dim line = reader.ReadLine() 
      While (Not String.IsNullOrWhiteSpace(line)) 
       Dim question = New Question 
       question.Question = line 
       question.Choice1 = reader.ReadLine() 
       question.Choice2 = reader.ReadLine() 
       question.Choice3 = reader.ReadLine() 
       question.Choice4 = reader.ReadLine() 
       question.Answer = reader.ReadLine() 
       listOfQuestions.Add(question) 
       line = reader.ReadLine() 
      End While 
     End Using 

     If listOfQuestions.Count > 0 Then 
      LoadQuestion(0) 
     End If 
    End Sub 

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
     Reset_all() 
    End Sub 

    Sub LoadQuestion(questionIndex As Integer) 

     Dim question = listOfQuestions(questionIndex) 
     currentQuestion = questionIndex 
     If listOfQuestions.Count - 1 = currentQuestion Then 

     End If 
     With question 
      Label3.Text = ques 
      Label1.Text = .Question 
      RadioButton1.Text = .Choice1 
      RadioButton2.Text = .Choice2 
      RadioButton3.Text = .Choice3 
      RadioButton4.Text = .Choice4 
      anskey = .Answer 
     End With 
    End Sub 

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
     If (SCORE > 0) Then 
      SCORE -= 1 
     End If 
     If (currentQuestion > 0) Then 
      If (ques > 0) Then 
       ques -= 1 
       LoadQuestion(currentQuestion - 1) 
      End If 
     End If 
    End Sub 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     If (anskey = "a" And RadioButton1.Checked = True Or anskey = "b" And RadioButton2.Checked = True Or anskey = "c" And RadioButton3.Checked = True Or anskey = "d" And RadioButton4.Checked = True) Then 
      SCORE += 1 
     End If 

     If (currentQuestion < listOfQuestions.Count - 1) Then 
      If (ques <= 99) Then 
       ques += 1 
       LoadQuestion(currentQuestion + 1) 
      End If 
     End If 
    End Sub 
    Private Sub Quiz_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing 
     Dashboard.Show() 
     Me.Hide() 
    End Sub 
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
     ProgressBar1.Value += 1 
     val -= 1 
     Label2.Text = val & " Sec" 
     If ProgressBar1.Value = ProgressBar1.Maximum Then 
      Timer1.Enabled = False 

     End If 
     If ProgressBar1.Value > 23 Then 
      SendMessage(ProgressBar1.Handle, 1040, 2, 0) 
      Button3.Show() 

     End If 
     If ProgressBar1.Value = 30 Then 

     End If 
    End Sub 

    Private Sub SubmitResult() 
     MsgBox("You have Scored " + SCORE.ToString + " Out of 100") 
    End Sub 
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 
     Dim Re As Integer = MsgBox("Are you sure you want to submit?", 
    vbYesNo, "Submit") 
     If (Re = 6) Then 
      SubmitResult() 
      Try 
       Me.Close() 
       Dashboard.Show() 
      Catch ex As Exception 
      End Try 
     End If 
    End Sub 

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click 

    End Sub 
End Class 
Public Class Question 

    Public Property Question As String 
    Public Property Choice1 As String 
    Public Property Choice2 As String 
    Public Property Choice3 As String 
    Public Property Choice4 As String 
    Public Property Answer As String 

End Class 
+5

* "Donnez-moi le code" * est pas la bienvenue ici SO. S'il vous plaît pensez à quelque chose, essayez quelque chose et codez quelque chose. Une fois cela fait, partagez avec nous votre code afin que nous puissions vous aider. – equisde

+2

@ExtremeDimension - S'il vous plaît ne pas lier à des sites externes - mettez votre code dans votre question. Si le site externe disparaît ou que les URL sont brisées, la question se pose également. – Enigmativity

Répondre

0
  • Tout d'abord, en utilisant la fonction File.ReadAllLines() pour obtenir un tableau de lignes dans le fichier texte avec des questions. Ensuite, vous pouvez y accéder facilement.

  • Si vous observez, l'index de la ligne de la question sera (n - 1) * 6, où n est le numéro de la question. Une fois que vous obtenez que les indices des options sont données par:

    i + 1 
    i + 2 
    i + 3 
    i + 4 
    

    i = (n - 1) * 6. La clé de réponse est donnée par:

    i + 5 
    

Cela devrait vous commencer. Si vous êtes coincé, laisser un commentaire :)

  • donc vous faire la première partie par:

    Dim lines() As String = File.ReadAllLines("<yourQuestions.txt") 
    
  • Ensuite, vous pouvez générer un nombre aléatoire dans la plage désirée à l'aide:

    Dim questionNumber As Integer = Random.Next(1, (lines.Length/6) + 1) 
    
  • Après cela, vous pouvez récupérer la question, les options et la clé de réponse par:

    Dim i As Integer = (questionNumber - 1) * 6 
    Dim question As String = lines(i) 
    Dim options() As String = {lines(i + 1), lines(i + 2), lines(i + 3), lines(i + 4)} 
    Dim answerKey As String = lines(Integer.Parse(lines(i + 5))) 
    

Vous pouvez également envisager la création d'une classe Question:

Public Class Question 

    Public Property Question As String 
    Public Property Choice1 As String 
    Public Property Choice2 As String 
    Public Property Choice3 As String 
    Public Property Choice4 As String 
    Public Property Answer As String 

    Public Sub New(q As String, c1 As String, c2 As String, c3 As String, c4 As String, ans As String) 
     Question = q 
     Choice1 = c1 
     Choice2 = c2 
     Choice3 = c3 
     Choice4 = c4 
     Answer = ans 
    End Sub 

End Class 

Ensuite, vous pouvez affecter les propriétés.

Une autre alternative (peut-être mieux dans la performance, en fait, je pense qu'il devrait être) serait d'utiliser la fonction File.ReadLines() et utiliser les Take() et Skip() méthodes d'extension de IEnumerable<T> (LINQ):

Dim questionNumber As Integer = Random.Next(1, (File.ReadLines("<yourQuestions.txt").Count()/6) + 1) 
Dim blockLines = File.ReadLines("<yourQuestions.txt").Skip((questionNumber - 1) * 6).Take(6) 
Dim currentQuestion As New Question(blockLines(0), blockLines(1), blockLines(2), blockLines(3), blockLines(4), blockLines(blockLines(5))) 
+0

[File.ReadLines] (https://msdn.microsoft.com/en-us/library/system.io.file.readlines%28v=vs.110%29.aspx) est un meilleur choix. – Enigmativity

+0

@Farhan c'est le fichier vb pour question http://pastebin.com/uRS8zn6h –

0

Je voudrais faire une classe « question » comme ceci:

public Class Question 

public questionas String 
public answer1 as String 
public answer2 as String 
public answer3 as String 
public answer4 as String 
public correctAnswer as integer 

public sub new(que as string, a1 as string, a2 as string, a3 as string, a4 as string, answer as integer) 
    question= que 
    answer1=a1 
    answer2=a2 
    answer3=a3 
    answer4=a4 
    correctAnswer=answer 
end sub 


end Class 

charger maintenant toutes vos questions dans le programm comme ceci:

Imports System 
Imports System.IO 

Class MainWindow 
    private listQuestions as List(Of Question) 

Public Sub window_loaded() handles MainWindow.loaded 
    listQuestions = loadAllQuestions()   
End Sub 



private function loadAllQuestions() as List(Of Question) 
Dim str() As String 
    Try 
     ' Open the file using a stream reader. 
     Using sr As New StreamReader("example.txt") 
      Dim line As String 
      ' Read the stream to a string and write the string to the console. 

      line = sr.ReadToEnd() 
      Str = line.Split(vbNewLine) 
     End Using 
    Catch e As Exception 
     Console.WriteLine("The file could not be read:") 
     Console.WriteLine(e.Message) 
    End Try 

    'So now put the questions in your list: 

    dim list as new List(Of Question) 

    For i = 0 to str.count - 1 
     if (i+1) mod 5 = 0 then 'is divible without rest by 6 
      list.add(new Question(str(i-5), str(i-4), str(i-3), str(i-2), str(i-1), str(i)) 
     end if 

    next 

    return list 



end sub 

'Load a random question: 
private sub btNext_click() handles btNext.click() 
    dim ranQuestion as Question 

    dim r as new random 

    ranQuestion = listFragen.item(r.next(0,listQuestions.count)) 


End Class 

J'espère que je pourrais vous aider.Pour éviter que le programm peut montrer la même question son votre travail :)

+0

Recommandez-vous vraiment d'écrire 'Catch e As Exception' dans le code actuel? C'est à peu près aussi bon que d'utiliser 'Goto'. – Enigmativity

+0

Le code thad vient de msdn.com (juste copié) –

+0

C'est un code horrible. MSDN devrait être triste pour lui-même. – Enigmativity

0

Pour commencer, chargez vos questions ainsi:

Dim questions = _ 
    File _ 
     .ReadLines("questions.txt") _ 
     .Select(Function (x, n) New With { .Line = X, .Index = n }) _ 
     .GroupBy(Function (xn) xn.Index \ 6, Function (xn) xn.Line) _ 
     .Select(Function (xs) New Question() With _ 
     { _ 
      .Question = xs(0), _ 
      .Choice1 = xs(1), _ 
      .Choice2 = xs(2), _ 
      .Choice3 = xs(3), _ 
      .Choice4 = xs(4), _ 
      .Answer = xs(5) _ 
     }) _ 
     .ToArray() 

Cela vous donnera un tableau pour vos questions:

questions

Ensuite, vous devez créer un fichier "queue.txt" qui contient les indices de vos questions dans un ordre aléatoire que vous souhaitez les afficher dans Voici comment créer votre file d'attente.

Dim rnd = New Random() 

Dim queue = _ 
    Enumerable _ 
     .Range(0, questions.Length) _ 
     .OrderBy(Function (n) rnd.Next()) _ 
     .Select(Function (n) n.ToString()) _ 
     .ToArray() 

File.WriteAllLines("queue.txt", queue) 

Ensuite, lorsque vous chargez votre programme, vous pouvez lire ce fichier et choisissez la prochaine question à poser, et enregistrez le fichier, en sautant la première question pour la prochaine fois, comme ceci:

Dim queue = File.ReadAllLines("queue.txt") 

Dim questionToAsk As Question = questions(Integer.Parse(queue.First())) 

File.WriteAllLines("queue.txt", queue.Skip(1)) 

Il Ce serait à vous de vous assurer que le fichier est créé quand il n'existe pas et d'écrire le code qui vérifie si vous avez posé toutes les questions et devez recréer la file d'attente.

+0

Awful :(code J'ai eu la réponse –

+0

@ExtremeDimension - Que voulez-vous dire par "Awful :(code."? – Enigmativity

+0

Je pense qu'il veut dire que c'est bien au-delà de son niveau, et 1) Il ne comprend pas et n'est pas prêt à apprendre 2) Il se fiche de savoir qui il offense –

0

Voici les bases d'une classe utilisant XML.

Public Class QuestionAndAnswer 

    'the container for all questions/answers 
    Private ReadOnly qa As XElement = <QandA></QandA> 
    'the container for a question and some number of possible answers 
    Private ReadOnly ent As XElement = <entry></entry> 
    'the question 
    Private ReadOnly aquestion As XElement = <question></question> 
    'an answer - the c attribute will be "y" for the correct answer 
    Private ReadOnly ananswer As XElement = <answer c=""></answer> 

    Private theQA As XElement 

    Public Sub New() 
     Me.theQA = New XElement(qa) 'set up 
    End Sub 

    Public Sub New(path As String) 
     Me.theQA = XElement.Load(path) 
    End Sub 

    Public Sub Save(path As String) 
     Me.theQA.Save(path) 
    End Sub 

    Private Function AddQuestion(theQuestion As String, correctAnsw As String) As XElement 
     Dim e As New XElement(ent) 
     Dim q As New XElement(aquestion) 
     Dim a As New XElement(ananswer) 
     q.Value = theQuestion 
     a.Value = correctAnsw 
     [email protected] = "y" 
     e.Add(q) 
     e.Add(a) 
     Me.theQA.Add(e) 
     Return e 
    End Function 

    Public Function AddQuestion(theQuestion As String, correctAnsw As String, 
           ans1 As String) As XElement 
     Dim e As XElement = Me.AddQuestion(theQuestion, correctAnsw) 
     Dim a As New XElement(ananswer) 
     a.Value = ans1 
     e.Add(a) 
     Return e 
    End Function 

    Public Function AddQuestion(theQuestion As String, correctAnsw As String, 
           ans1 As String, ans2 As String) As XElement 
     Dim e As XElement = Me.AddQuestion(theQuestion, correctAnsw, ans1) 
     Dim a As New XElement(ananswer) 
     a.Value = ans2 
     e.Add(a) 
     Return e 
    End Function 

    Public Function AddQuestion(theQuestion As String, correctAnsw As String, 
           ans1 As String, ans2 As String, ans3 As String) As XElement 
     Dim e As XElement = Me.AddQuestion(theQuestion, correctAnsw, ans1, ans2) 
     Dim a As New XElement(ananswer) 
     a.Value = ans3 
     e.Add(a) 
     Return e 
    End Function 

    Public Function AddQuestion(theQuestion As String, correctAnsw As String, 
           ans1 As String, ans2 As String, ans3 As String, ans4 As String) As XElement 
     Dim e As XElement = Me.AddQuestion(theQuestion, correctAnsw, ans1, ans2, ans3) 
     Dim a As New XElement(ananswer) 
     a.Value = ans4 
     e.Add(a) 
     Return e 
    End Function 

    Private Shared prng As New Random 
    Public LastQuestionAnswer As String 

    Public Function RandomQuestion() As String 
     Dim q As XElement = Me.SelectRandomQ 
     If q IsNot Nothing Then 
      Dim rv As New System.Text.StringBuilder 
      rv.AppendLine(q.<question>.Value) 
      rv.AppendLine() 
      Dim ie As IEnumerable(Of XElement) 
      ie = From qa In q.<answer> 
       Select qa 

      ie = ie.OrderBy(Function() prng.Next(q.<answer>.Count)) 

      Dim x As Integer = 1 

      For Each a As XElement In ie 
       If [email protected] = "y" Then 
        Me.LastQuestionAnswer = x.ToString 
       End If 
       rv.AppendFormat("{0}. {1}", x, a.Value) 
       rv.AppendLine() 
       x += 1 
      Next 
      rv.AppendLine() 
      Me.LastQuestionAnswer = Me.LastQuestionAnswer.Insert(0, rv.ToString) 
      Debug.WriteLine(Me.LastQuestionAnswer) 
      Return rv.ToString 
     End If 
     Return "" 
    End Function 

    Private Function SelectRandomQ() As XElement 
     If Me.theQA IsNot Nothing AndAlso Me.theQA.<entry>.Count > 0 Then 
      Dim ie As IEnumerable(Of XElement) 
      ie = From ent In Me.theQA.Elements 
       Where [email protected] <> "y" 
       Select ent 
      Dim rv As XElement = ie(prng.Next(ie.Count)) 
      [email protected] = "y" 
      Return rv 
     End If 
     Return Nothing 
    End Function 
End Class 

Une forme avec un richtextbox et un bouton montre qu'il

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim foo As New QuestionAndAnswer 
    foo.RandomQuestion() 

    foo.AddQuestion("Which Vietnam War film directed by Francis Ford Coppola followed a screenplay based on famous adventure story Heart of Darkness?", 
        "Apocalypse Now", 
        "Good Morning Vietnam", 
        "Born on the Fourth of July", 
        "Band of Brothers") 

    foo.AddQuestion("What was the name of the baseball pitcher that hit a bird with a pitch during a 2001 Spring Training game?", 
        "Randy Johnson", 
        "Mike Mussina", 
        "Roger Clemens", 
        "Greg Maddux", 
        "Johan Santana") 

    foo.AddQuestion("The third largest desert in the world is the Sahara, what is the first?", 
        "Antarctic", 
        "Gobi", 
        "Sonoran") 

    foo.AddQuestion("How many US presidents have died while in office?", 
        "8", 
        "6", 
        "7") 

    foo.AddQuestion("If x is the first of five consecutive odd numbers > 0, then what is their average?", 
        "x + 4", 
        "x", 
        "x + 1", 
        "x + 3") 

    Dim qa As New System.Text.StringBuilder 
    foo.RandomQuestion() 
    qa.AppendLine(foo.LastQuestionAnswer) 
    qa.AppendLine() 
    foo.RandomQuestion() 
    qa.AppendLine(foo.LastQuestionAnswer) 
    qa.AppendLine() 
    foo.RandomQuestion() 
    qa.AppendLine(foo.LastQuestionAnswer) 
    RichTextBox1.Text = qa.ToString 
End Sub