Sdílet prostřednictvím


Krok 4: Přidat metodu CheckTheAnswer()

Váš kvíz musí ověřit, zda uživatel odpoví správně. Naštěstí psaní metod, které provádí jednoduché výpočty, jako je například metoda CheckTheAnswer(), není obtížné.

Poznámka

Pro uživatele, kteří používají jazyk Visual Basic, stojí za zmínku, že tato metoda vrátí hodnotu namísto obvyklého klíčového slova Sub, které budete používat místo klíčového slova Function. Je to skutečně tak jednoduché: Subs nevrací hodnotu, ale funkce ano.

Přidat metodu CheckTheAnswer()

  1. Přidejte metodu CheckTheAnswer(), která přidá addend1 a addend2 a ověří, zda je součet roven hodnotě v součtovém ovládacím prvku NumericUpDown. Pokud je součet roven, metoda vrátí hodnotu pravda; pokud ne, vrátí hodnotu nepravda. Váš kód by měl vypadat takto.

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

    Program potřebuje volat tuto metodu k ověření, zda uživatel odpověděl správně. To provedete přidáním do vašeho příkazu if else. Příkaz vypadá následovně:

    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 If
    
    if (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
    }  
    
  2. Potom změníte obslužnou rutinu události časovače Tick ke zkontrolování odpovědi. Nová obslužná rutina události s kontrolou odpovědí by měla obsahovat následující.

    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 Sub
    
    private 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;
        }
    }
    

    Nyní jestli obslužná rutina události časovače zjistí, že uživatel odpověděl správně, obslužná rutina události zastaví časovač, zobrazí zprávu s blahopřáním a udělá tlačítko Start opět k dispozici.

  3. Uložte program a spusťte jej. Spusťte hru a zadejte správnou odpověď v úloze sčítání.

    Poznámka

    Zadáte-li odpověď, můžete si všimnout něčeho podivného na ovládacím prvku NumericUpDown. Pokud začnete psát bez výběru celé odpovědi, zbude nula a je nutné ji ručně odstranit. Opravu tohoto provedeme dále v tomto kurzu.

  4. Zadáte-li správnou odpověď, okno se zprávou by se mělo otevřít, tlačítko Start by mělo být k dispozici a časovač by se měl zastavit. Klikněte na tlačítko Start znovu a přesvědčte se, zda tato situace nastane.

Chcete-li pokračovat nebo znovu projít