CodeBlanks Logo Icon CodeBlanks

Skor hesaplama

PHP kursu · Orta seviye · Ders 45/115

foreach'le tüm soruları gezip doğru sayısını hesaplarsın.

Quiz sınıfı ve skor metodu

Birden çok soruyu tek pakette tutmak için Quiz sınıfını tanımlayacaksın. Bu sınıfın public array $questions property'si soru nesnelerini bir dizide tutar. Yapıcı metot bu diziyi alır ve property'ye atar.

Skoru hesaplamak için score(array $answers): int metodu eklersin. Bu metot bir foreach döngüsüyle tüm soruları gezer, her sorunun isCorrect metodunu çağırır ve doğru sayısını sayar. Parametre olan $answers dizisi, kullanıcının her soru için verdiği cevap indekslerini içerir.

Akümülatör deseni ile sayaç

Skor metodu içinde bir $correct sayacı tutarsın. Başlangıçta $correct = 0; olur. foreach ile her soruyu gezerken, cevap doğruysa $correct++ ile sayacı bir artırırsın. Son olarak return $correct; ile toplamı döndürürsün.

Bu akümülatör (accumulator) desenidir: bir değişken başlatılır, döngü içinde koşullu olarak artırılır ve en son döndürülür. Skor, ortalama, toplam gibi hesaplar için yaygın bir desendir.

Uygulama Quiz sınıfını ve score metodunu ekle

Question sınıfından sonra Quiz sınıfını tanımla. İçine public array $questions; property'sini ve public function __construct(array $questions = []) yapıcı metodunu ekle; yapıcıda $this->questions = $questions; ata. Sonra public function score(array $answers): int metodunu ekle. İçinde $correct = 0; ile başlat, foreach ($this->questions as $i => $q) ile gez, if ($q->isCorrect($answers[$i] ?? -1)) ile kontrol et, doğruysa $correct++ artır. En son return $correct; ile döndür. Son satırları $quiz = new Quiz([...]); ile iki soru yükle ve echo $quiz->score([0, 1]); ile skoru yazdır.

Örnek çözümü göster
int { $correct = 0; foreach ($this->questions as $i => $q) { if ($q->isCorrect($answers[$i] ?? -1)) { $correct++; } } return $correct; } } yaz. $quiz = new Quiz([ new Question("PHP nedir?", ["Betik dili", "Veritabanı", "Tarayıcı"], 0), new Question("Hangisi döngü?", ["if", "for", "echo"], 1), ]); ile iki soru yükle, echo $quiz->score([0, 1]); ile yazdır.
Örnek çözüm:
<?php
// Question + Quiz sınıfları ile skor hesaplama
class Question {
    public string $text = "";
    public array $options = [];
    public int $answerIndex = 0;

    public function __construct(string $text, array $options, int $answerIndex) {
        $this->text = $text;
        $this->options = $options;
        $this->answerIndex = $answerIndex;
    }

    public function isCorrect(int $choice): bool {
        return $choice === $this->answerIndex;
    }
}

class Quiz {
    public array $questions;

    public function __construct(array $questions = []) {
        $this->questions = $questions;
    }

    public function score(array $answers): int {
        $correct = 0;
        foreach ($this->questions as $i => $q) {
            if ($q->isCorrect($answers[$i] ?? -1)) {
                $correct++;
            }
        }
        return $correct;
    }
}

$quiz = new Quiz([
    new Question("PHP nedir?", ["Betik dili", "Veritabanı", "Tarayıcı"], 0),
    new Question("Hangisi döngü?", ["if", "for", "echo"], 1),
]);
echo $quiz->score([0, 1]);

Kendini test et

  1. foreach döngüsü ne işe yarar?

    • Yalnızca sayıları bir artırır
    • Sınıf tanımlar
    • Dosya okur
    • Bir dizinin tüm öğelerini sırayla gezer
    Cevabı göster

    Bir dizinin tüm öğelerini sırayla gezer — Doğru. foreach, bir dizinin tüm öğelerini sırayla gezer; her öğe için döngü gövdesini çalıştırır.

  2. foreach döngüsünün öğe kısmını tamamla

    foreach ($this->questions as ____) {
        if ($q->isCorrect($answers[$i] ?? -1)) {
            $correct++;
        }
    }
    Cevabı göster
    foreach ($this->questions as $i => $q) {
        if ($q->isCorrect($answers[$i] ?? -1)) {
            $correct++;
        }
    }
  3. Skor hesaplama kavramlarını eşleştir

    foreach
    Döngü yapısı
    $correct
    Sayaç değişkeni
    $correct++
    Sayaç artırma
    return $correct
    Toplamı döndürür

Orta seviye bölümündeki tüm dersler