Krok 4: Přidejte metodu CheckTheAnswer()
Ve čtvrté části tohoto tutoriálu budete zapisovat metodu CheckTheAnswer(), která určuje, zda jsou odpovědi na matematické úlohy správné.Toto téma je součástí řady výukových programů o základních principech kódování.Přehled tutoriálu naleznete v tématu Tutoriál 2: Vytvoření matematického kvízu s časovým limitem.
[!POZNÁMKA]
Pokud jste postupovali společně s jazykem Visual Basic, budete používat klíčové slovo Function namísto obvyklého klíčového slova Sub vzhledem k tomu, že tato metoda vrátí hodnotu.Je to skutečně tak jednoduché: sub nevrací hodnotu, ale funkce ano.
Ověření toho, zda jsou odpovědi správné
Přidejte metodu CheckTheAnswer().
Pokud tato metoda je volána, přidá hodnoty addend1 a addend2 a porovnává výsledek s hodnotou v součtu ovládacího prvku NumericUpDown.Pokud jsou hodnoty stejné, vrátí metoda hodnotu true.V opačném případě vrátí metoda hodnotu false.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; }
Dále zkontrolujte odpověď aktualizací kódu v metodě pro obslužnou rutiny události Tick časovače pro zavolání nové metody CheckTheAnswer().
Přidejte následující kód do příkazu 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; } }
Pokud je odpověď správná, CheckTheAnswer() vrátí true.Obslužná rutina události zastaví časovač, zobrazí zprávu s gratulací a pak opět zpřístupní tlačítko Start.V opačném případě kvíz pokračuje.
Uložte program, spusťte jej, spusťte kvíz a zadejte správnou odpověď na úlohu sčítání.
[!POZNÁMKA]
Pokud zadáte vaši odpověď, musíte buď vybrat výchozí hodnotu dříve, než začnete zadávat odpověď nebo musíte nulu odstranit ručně.Opravu tohoto chování provedeme dále v tomto kurzu.
Pokud zadáte správnou odpověď, otevře okno se zprávou, že tlačítko Start bude k dispozici a časovač se zastaví.
Pokračování nebo kontrola
Přechod na další výukový program naleznete v tématu Krok 5: Přidejte obslužné rutiny události pro ovládací prvky NumericUpDown.
K návratu do předchozího kroku výukového programu přejděte na Krok 3: Přidejte časovač odpočítávání.