خطوة 4: إضافة أسلوب CheckTheAnswer()
يحتاج الاختبار الخاص بك إلى التحقق من ما إذا أجاب المستخدم بشكل صحيح. ولحسن الحظ، كتابة أساليب إجراء عمليات حسابية بسيطة، مثل CheckTheAnswer() أسلوب غير صعبة.
ملاحظة
لهؤلاء الذين يفهمون Visual Basic ، لا تفيدهم شيئاً لعدم إعادة القيمة في هذه الطريقة, بدلاً من الكلمة الأساسية Sub سوف يتم استخدام الكلمة الأساسيةFunction بدلاً من ذلك. إنها حقاً بسيطة, مثل: subs لا ترجع القيمة ولكن الدالات تفعل.
لإضافة أسلوب CheckTheAnswer()
قم بإضافة أسلوبCheckTheAnswer() الذي يضيف addend1 و addend2 والتحقق من ما إذا كان المجموع يساوي القيمة في مجموع عنصر التحكم NumericUpDown. في حالة تساوي المجموع يعود الأسلوب صواب; إذا كان غير ذلك، يعود خطأ. يجب أن تشبه التعليمة البرمجية المثال التالي.
''' <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; }
يحتاج البرنامج إلى استدعاء هذا الأسلوب للتحقق ما إذا أجاب المستخدم بشكل صحيح. يمكنك القيام بذلك عن طريق الإضافة إلى العبارةif else . تظهر العبارة كما يلي.
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 }
بعد ذلك، يمكنك تعديل تجزئة حدث معالج لدَقة جهاز ضبط الوقت التحقق من الإجابة. معالج الأحداث الجديد المرفق بتدقيق للأجوبة يجب أن تتضمن ما يلي.
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; } }
الآن إذا عثر معالج الأحداث الخاص بجهاز ضبط الوقت بأن المستخدم أجاب بشكل صحيح, يقوم معالج الأحداث بإيقاف جهاز ضبط الوقت يعرض رسالة تهنئة و يجعل زر ابدأ متوفر مرة أخرى.
حفظ البرنامج وتشغيلها. إبدأ اللعبة ثم إطبع الإجابة الصحيحة لمشكلة الإضافة.
ملاحظة
عند كتابتك للإجابة قد تلاحظ شيئاً غريباً حول عصر التحكم NumericUpDown. إذا كنت تبدأ دون تحديد الإجابات بأكملها ، يبقى الصفر و يجب يحذفه يدوياً. سيتم تصحيح هذا لاحقاً في هذا البرنامج التعليمي.
عند كتابة الإجابات الصحيحة يظهر مربع الرسالة ، و زر ابدأ يجب أن يكون متوفراً كما يجب إيقاف جهاز ضبط الوقت. انقر فوق زر إبدا مرة أخرى ثم تأكد من أن يحدث هذا.
للمتابعة أو للمراجعة
للانتقال إلى الخطوة التالية بالبرنامج التعليمي, راجع خطوة 5: اضف معالجات أحداث دخول لعناصر تحكم ب NumericUpDown.
للعودة إلى الخطوة السابقة بالبرنامج التعليمي راجع خطوة 3: أضف جهاز ضبط العد التنازلي..