Krok 4. Dodawanie metody CheckTheAnswer()

W czwartej części tego samouczka będziesz pisać metodę CheckTheAnswer(), która określa, czy odpowiedzi na pytania matematyczne są poprawne.Ten temat jest częścią serii samouczków na temat podstawowych pojęć kodowania.Aby uzyskać przegląd samouczka, zobacz Samouczek 2: Utworzenie kwizu matematycznego z limitem czasu.

[!UWAGA]

Jeśli piszesz w Visual Basic, będziesz używał słowa kluczowego Function zamiast zwykłego słowa kluczowego Sub, ponieważ metoda ta zwraca wartość.To jest naprawdę proste: sub nie zwraca wartości, natomiast funkcja zwraca wartość.

Aby sprawdzić, czy odpowiedzi są poprawne

  1. Dodaj metodę CheckTheAnswer().

    Gdy ta metoda jest wywoływana, dodaje wartości addend1 i addend2 i porównuje wynik z wartością w sumie formantu NumericUpDown.Jeśli wartości są równe, metoda zwraca wartość true.W przeciwnym razie metoda zwraca wartość false.Kod powinien wyglądać następująco.

    ''' <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;
    }
    

    Następnie sprawdzisz odpowiedź, aktualizując kod w metodzie dla programu obsługi zdarzeń timera Tick, aby wywołać nową metodę CheckTheAnswer().

  2. Dodaj następujący kod do instrukcji if else.

    Private Sub Timer1_Tick() Handles Timer1.Tick
    
        If CheckTheAnswer() Then 
            ' If CheckTheAnswer() returns true, then 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 
            ' If CheckTheAnswer() return false, keep counting 
            ' down. Decrease the time left by one second and  
            ' display the new time left by updating the  
            ' Time Left label.
            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 Sub
    
    private void timer1_Tick(object sender, EventArgs e)
    {
        if (CheckTheAnswer())
        {
            // If CheckTheAnswer() returns true, then 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)
        {
           // If CheckTheAnswer() return false, keep counting 
           // down. 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;
        }
    }
    

    Jeśli odpowiedź jest poprawna, CheckTheAnswer() zwraca true.Program obsługi zdarzeń zatrzymuje timer, pokazuje komunikat z gratulacjami i następnie ponownie udostępnia przycisk Start.W przeciwnym razie quiz trwa nadal.

  3. Zapisać program, uruchom go, uruchom quiz i podaj poprawną odpowiedź na problem dodawania.

    [!UWAGA]

    Po wprowadzeniu swojej odpowiedzi, musisz wybrać wartość domyślną przed rozpoczęciem wprowadzania odpowiedzi lub musisz usunąć zero ręcznie.W dalszej części tego samouczka skorygujesz to zachowanie.

    Po podaniu poprawnej odpowiedzi zostanie otwarte okno komunikatu, przycisk Start stanie się dostępny, a timer się zatrzyma.

Aby kontynuować lub przeglądnąć