Quiz için yazılan komut çalışmıyor

arda.nb01

Centipat
Katılım
24 Ocak 2024
Mesajlar
281
Daha fazla  
Cinsiyet
Erkek
Merhaba bir kodum var kodumdaki mantık şu; kullanıcı eğer soruyu doğru cevapladıysa buton yeşil olsun. Fakat yanlış işaretlediği ise buton kırmızı olsun ve doğru butonu yeşil olarak göstersin. Nerede hata yapıyorum?

Java:
package com.example.denemebstr;

import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class QuizActivity extends AppCompatActivity {

 private TextView questionText, timerText;
 private Button answerButton1, answerButton2, answerButton3, nextButton;

 private int currentQuestionIndex = 0;
 private List<Question> questionList;
 private CountDownTimer timer;

 private static final long QUESTION_TIME = 6000; // 6 saniye.

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_quiz);

 questionText = findViewById(R.id.questionText);
 timerText = findViewById(R.id.timerText);
 answerButton1 = findViewById(R.id.answerButton1);
 answerButton2 = findViewById(R.id.answerButton2);
 answerButton3 = findViewById(R.id.answerButton3);
 nextButton = findViewById(R.id.nextButton);

 questionList = new ArrayList<>();
 loadQuestions();

 displayQuestion();

 answerButton1.setOnClickListener(v -> checkAnswer(0));
 answerButton2.setOnClickListener(v -> checkAnswer(1));
 answerButton3.setOnClickListener(v -> checkAnswer(2));

 nextButton.setOnClickListener(v -> {
 currentQuestionIndex++;
 if (currentQuestionIndex < questionList.size()) {
 displayQuestion();
 } else {
 // Quiz tamamlandı.
 finish(); // veya sonuçları göster.
 }
 });
 }

 private void loadQuestions() {
 questionList.add(new Question("Hangi renk gökyüzünün rengidir?", "Mavi", "Yeşil", "Kırmızı", 0));
 questionList.add(new Question("Hangi gezegen Dünya'nın uydusudur?", "Mars", "Venüs", "Ay", 2));
 questionList.add(new Question("Hangi element suyun bileşenidir?", "Oksijen", "Hidrojen", "Karbon", 0));
 Collections.shuffle(questionList); // Soruları karıştır.
 }

 private void displayQuestion() {
 resetButtonColors(); // Buton renklerini sıfırla.

 Question question = questionList.get(currentQuestionIndex);
 questionText.setText(question.getQuestion());
 answerButton1.setText(question.getAnswers().get(0));
 answerButton2.setText(question.getAnswers().get(1));
 answerButton3.setText(question.getAnswers().get(2));
 nextButton.setVisibility(View.GONE); // Sonraki butonunu gizle.

 startTimer(); // Zamanlayıcıyı başlat.
 }

 private void startTimer() {
 timer = new CountDownTimer(QUESTION_TIME, 1000) {

 @Override
 public void onTick(long millisUntilFinished) {
 timerText.setText("Süre: " + millisUntilFinished / 1000);
 }

 @Override
 public void onFinish() {
 // Süre doldu, kullanıcı cevap vermediyse.
 checkAnswer(-1);
 }
 }.start();
 }

 private void checkAnswer(int selectedAnswerIndex) {
 if (timer != null) {
 timer.cancel(); // Zamanlayıcıyı durdur.
 }

 Question question = questionList.get(currentQuestionIndex);
 int correctIndex = question.getCorrectAnswerIndex();

 // Önce butonları gri yapalım.
 resetButtonColors();

 // Seçilen buton ve doğru cevabı bul.
 Button selectedButton = getButtonByIndex(selectedAnswerIndex);
 Button correctButton = getButtonByIndex(correctIndex);

 if (selectedAnswerIndex == correctIndex) {
 // Doğru cevap.
 if (selectedButton != null) {
 selectedButton.setBackgroundColor(getResources().getColor(R.color.colorCorrect));
 }
 } else {
 // Yanlış cevap.
 if (selectedButton != null) {
 selectedButton.setBackgroundColor(getResources().getColor(R.color.colorWrong));
 }
 if (correctButton != null) {
 correctButton.setBackgroundColor(getResources().getColor(R.color.colorCorrect));
 }
 }

 nextButton.setVisibility(View.VISIBLE); // Sonraki butonunu göster.
 }

 private Button getButtonByIndex(int index) {
 switch (index) {
 case 0:
 return answerButton1;
 case 1:
 return answerButton2;
 case 2:
 return answerButton3;
 default:
 return null;
 }
 }

 private void resetButtonColors() {
 int defaultColor = getResources().getColor(R.color.colorPrimary);
 answerButton1.setBackgroundColor(defaultColor);
 answerButton2.setBackgroundColor(defaultColor);
 answerButton3.setBackgroundColor(defaultColor);
 }
}
 
Bu siteyi kullanmak için çerezler gereklidir. Siteyi kullanmaya devam etmek için çerezleri kabul etmelisiniz. Daha Fazlasını Öğren.…