Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Il quiz deve verificare se l'utente risponde correttamente.Fortunatamente, la scrittura di metodi che eseguono un calcolo semplice, quale il metodo CheckTheAnswer(), non è difficile.
Nota
Per coloro che utilizzano Visual Basic, tenere presente che poiché questo metodo restituisce un valore, anziché la parola chiave Sub solita si utilizzerà invece la parola chiave Function.È molto semplice: le subroutine non restituiscono un valore, ma le funzioni sì.
Per aggiungere il metodo CheckTheAnswer()
Aggiungere il metodo CheckTheAnswer(), che a sua volta aggiunge addend1 e addend2 e verifica se la somma è uguale al valore nel controllo NumericUpDown della somma.Se la somma è uguale, il metodo restituisce true. Altrimenti restituisce false.Il codice dovrebbe essere analogo al seguente.
''' <summary> ''' Check the answer to see if the user got everything right. ''' </summary> ''' <returns>True if the answer's correct, false otherwise.</returns> ''' <remarks></remarks> Public Function CheckTheAnswer() As Boolean If addend1 + addend2 = sum.Value Then Return True Else Return False End If End Function/// <summary> /// Check the answer to see if the user got everything right. /// </summary> /// <returns>True if the answer's correct, false otherwise.</returns> private bool CheckTheAnswer() { if (addend1 + addend2 == sum.Value) return true; else return false; }Il programma deve chiamare questo metodo per verificare se l'utente ha risposto correttamente.Per fare ciò, si aggiungono elementi all'istruzione if else.L'istruzione è analoga alla seguente.
If CheckTheAnswer() Then ' statements that will get executed ' if the answer is correct ElseIf timeLeft > 0 Then ' statements that will get executed ' if there's still time left on the timer Else ' statements that will get executed if the timer ran out End Ifif (CheckTheAnswer()) { // statements that will get executed // if the answer is correct } else if (timeLeft > 0) { // statements that will get executed // if there's still time left on the timer } else { // statements that will get executed if the timer ran out }Quindi, si modifica il gestore dell'evento Tick del timer per controllare la risposta.Il nuovo gestore dell'evento con la verifica della risposta deve includere gli elementi seguenti.
Private Sub Timer1_Tick() Handles Timer1.Tick If CheckTheAnswer() Then ' If the user got the answer right, stop the timer ' and show a MessageBox. Timer1.Stop() MessageBox.Show("You got all of the answers right!", "Congratulations!") startButton.Enabled = True ElseIf timeLeft > 0 Then ' Decrease the time left by one second and display ' the new time left by updating the Time Left label. timeLeft = timeLeft - 1 timeLabel.Text = timeLeft & " seconds" Else ' If the user ran out of time, stop the timer, show ' a MessageBox, and fill in the answers. Timer1.Stop() timeLabel.Text = "Time's up!" MessageBox.Show("You didn't finish in time.", "Sorry") sum.Value = addend1 + addend2 startButton.Enabled = True End If End Subprivate void timer1_Tick(object sender, EventArgs e) { if (CheckTheAnswer()) { // If the user got the answer right, stop the timer // and show a MessageBox. timer1.Stop(); MessageBox.Show("You got all the answers right!", "Congratulations"); startButton.Enabled = true; } else if (timeLeft > 0) { // Decrease the time left by one second and display // the new time left by updating the Time Left label. timeLeft--; timeLabel.Text = timeLeft + " seconds"; } else { // If the user ran out of time, stop the timer, show // a MessageBox, and fill in the answers. timer1.Stop(); timeLabel.Text = "Time's up!"; MessageBox.Show("You didn't finish in time.", "Sorry"); sum.Value = addend1 + addend2; startButton.Enabled = true; } }Ora se il gestore dell'evento del timer rileva che l'utente ha risposto correttamente, arresta il timer, visualizza un messaggio di congratulazioni e rende nuovamente disponibile il pulsante Avvia.
Salvare ed eseguire il programma.Avviare il gioco e digitare la risposta corretta al problema di addizione.
Nota
Quando si digita la risposta, è possibile che venga riscontrato un comportamento insolito riguardo al controllo NumericUpDown.Se si inizia a digitare senza selezionare la risposta intera, lo zero resta ed è necessario eliminarlo manualmente.Si correggerà questo problema più avanti in questa esercitazione.
Quando si digita la risposta corretta, deve essere visualizzata la finestra di messaggio, deve essere disponibile il pulsante Avvia e il timer si deve arrestare.Fare nuovamente clic sul pulsante Avvia e accertarsi che si verifichino questi eventi.
Per continuare o rivedere l'esercitazione
Per andare al passaggio successivo dell'esercitazione, vedere Passaggio 5: aggiungere gestori di eventi Enter per i controlli NumericUpDown.
Per tornare al passaggio precedente dell'esercitazione, vedere Passaggio 3: aggiungere un timer per il conto alla rovescia.