1

我有一个有 10 个限时问题的问答游戏。一个错误的答案应该输出 0 分。正确的答案应该是输出 20,000 毫秒的剩余时间,转换为 100 分制。

目前该游戏适用于第一款游戏。然后,如果用户继续玩第二次,每个问题的分数就会变得不正确。例如,错误的答案开始有从 1 到 100 的各种分数,并且正确的答案有时有 0 分,有时也有错误的总分。所以我猜一些变量可能不会在第二场比赛中重置,所以它导致积分有点偏离。

我发布了所有处理算法的代码。我的问题是,无论用户在关闭应用程序之前玩了多少次,我如何才能让这段代码输出正确的答案。

QuestionView.java

public class QuestionView extends Activity  {

    int correctAnswers = 0;
    int wrongAnswers = 0;
    int answer = 0;
    int i = 0;

    long score = 0;

    long startTime = 20000;
    long interval = 1000;
    long points;

    boolean timerHasStarted = false;

    String category;

    Button answer1, answer2, answer3, answer4;
    TextView question, pointCounter, questionNumber, timeCounter, timeremaining;
    AdView adView;

    ArrayList<Question> queries;
    public static ArrayList<Long> pointsPerQuestion = new ArrayList<Long>(10);
    Timer cdTimer;

    ProgressBar bar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.questionview);

        answer1 = (Button)findViewById(R.id.answer1);
        answer2 = (Button)findViewById(R.id.answer2);
        answer3 = (Button)findViewById(R.id.answer3);
        answer4 = (Button)findViewById(R.id.answer4);

        question = (TextView)findViewById(R.id.question);

        category = getIntent().getStringExtra("category");
        queries = getIntent().getParcelableArrayListExtra("queries");

        questionNumber = (TextView)findViewById(R.id.questionnumber);
        timeremaining = (TextView)findViewById(R.id.timeremaining);

        cdTimer = new Timer(startTime, interval);

        bar = (ProgressBar)findViewById(R.id.progressbar);
        bar.setIndeterminate(false);
        bar.setMax(20000);

        tracker.sendView("/QuestionView - Started Quiz");

        loadQuestion();
    }

    public void loadQuestion() {

        if(i == 10) {

            cdTimer.cancel();
            endQuiz();

        } else {

            if(!timerHasStarted) {
                cdTimer.start();
                timerHasStarted = true;
            } else {
                cdTimer.start();
                timerHasStarted = false;
            }

            answer = queries.get(i).getCorrectAnswer();

            question.setText(queries.get(i).getQuery());

            answer1.setText(queries.get(i).getA1());
            answer2.setText(queries.get(i).getA2());
            answer3.setText(queries.get(i).getA3());
            answer4.setText(queries.get(i).getA4());

            answer1.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(0);
                    if(answer == 0) {
                        correctAnswers++;
                        correct();
                    } else {
                        wrongAnswers++;
                        incorrect();
                    }
                }
            });

            answer2.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(1);
                    if(answer == 1) {
                        correctAnswers++;
                        correct();
                    } else {
                        wrongAnswers++;
                        incorrect();
                    }
                }
            });

            answer3.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(2);
                    if(answer == 2) {
                        correctAnswers++;
                        correct();
                    } else {
                        wrongAnswers++;
                        incorrect();
                    }
                }
            });

            answer4.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(3);
                    if(answer == 3) {
                        correctAnswers++;
                        correct();
                    } else {
                        wrongAnswers++;
                        incorrect();
                    }
                }
            });
        } 
    }

    public ArrayList<Question> getQueries() {
        return queries;
    }

    public static ArrayList<Long> getPointsPerQuestion() {
        return pointsPerQuestion;
    }

    public void correct() {
        pointsPerQuestion.add(points); 
        score = score + points;
        i++;
        loadQuestion();
    }

    public void incorrect() {
        long zero = 0;
        pointsPerQuestion.add(zero);
        i++;
        loadQuestion();
    }

    public class Timer extends CountDownTimer {

        public Timer(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {
            points = 0;
            if(i >= 9) {
                cdTimer.cancel();
                pointsPerQuestion.add(points);
                endQuiz();
            } else {
                wrongAnswers++;
                incorrect();
            }
        }

        @Override
        public void onTick(long millisUntilFinished) {
            bar.setProgress((int) millisUntilFinished);
            points = millisUntilFinished / 200;
            timeremaining.setText("Score remaining: " + points);
            if(i < 10) {
                questionNumber.setText("Question " + (i + 1) + " of 10");
            }
        }
    }

    public void endQuiz() {
        Intent intent = new Intent(QuestionView.this, Results.class);
        intent.putExtra("correctAnswers", correctAnswers);
        intent.putExtra("wrongAnswers", wrongAnswers);
        intent.putExtra("score", score);
        intent.putExtra("pointsPerQuestion", pointsPerQuestion);
        intent.putParcelableArrayListExtra("queries", queries);
        intent.putExtra("category", category);
        startActivity(intent);
    }
}

这是一个Question对象:

exodus.add(new Question("6th", "3rd", "5th", "4th", 2, "Name that plague:  All livestock dies", -1, "Exodus 9:6"));

在我的Results班级中,显示每个问题的分数,这是显示分数的行:

scoreTV.setText("You received " + QuestionView.getPointsPerQuestion().get(x) + " out of a possible 100 points.");

其他一切都在我QuestionView上面。如果您还需要其他东西,请告诉我。

前 3 个字符串是答案 A(索引 0)、B(索引 1)、C(索引 2)和 D(索引 3)。第一个 int 是正确答案。第 5 个字符串是问题。第二个整数设置为用户在测验期间选择的答案。最后的字符串是找到答案的圣经经文。

4

0 回答 0