jeudi 22 septembre 2016

Calculating correct score on simple quiz application with CheckBox

I'm having trouble with displaying the correct score on my quiz application - specifically with the CheckBox. I have four choices but only two are correct (B & C). When all four choices are selected (incorrect) it still adds the points. How do I prevent this from happening and correctly score these CheckBoxes? Anyone know? Thanks!

package com.example.android.quantummechanicsquiz;

    import android.provider.MediaStore;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.text.Editable;
    import android.view.View;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends AppCompatActivity {

        int score = 0;

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


        public void radioButtonClicked(View view) {
            boolean checked = ((RadioButton) view).isChecked();

            switch (view.getId()) {
                case R.id.radio_one_correct:
                    if (checked) break;

                case R.id.radio_one_b:
                    if (checked) break;

                case R.id.radio_one_c:
                    if (checked) break;

                case R.id.radio_one_d:
                    if (checked) break;

                case R.id.radio_two_a:
                    if (checked) break;

                case R.id.radio_two_b:
                    if (checked) break;

                case R.id.radio_two_correct:
                    if (checked) break;

                case R.id.radio_two_d:
                    if (checked) break;

                case R.id.radio_four_a:
                    if (checked) break;

                case R.id.radio_four_b:
                    if (checked) break;

                case R.id.radio_four_correct:
                    if (checked) break;

                case R.id.radio_four_d:
                    if (checked) break;

                case R.id.radio_five_a:
                    if (checked) break;

                case R.id.radio_five_b:
                    if (checked) break;

                case R.id.radio_five_c:
                    if (checked) break;

                case R.id.radio_five_correct:
                    if (checked) break;


            }

        }


        private int calculateScore(boolean answerOne, boolean answerTwo, boolean answerThreeA,
                                   boolean answerThreeB, boolean answerThreeC, boolean answerThreeD,
                                   boolean answerFour, boolean answerFive, Editable answerSix) {
            score = 100;

            if (answerOne) {


            } else {

                score = score - 100 / 6;
            }

            if (answerTwo) {


            } else {

                score = score - 100 / 6;
            }


            if (answerThreeB && answerThreeC) {

            } else {

                score = score - 100/6;
            }




            if (answerFour) {


            } else {

                score = score - 100 / 6;
            }

            if (answerFive) {


            } else {

                score = score - 100 / 6;
            }


            if (answerSix.toString().equals("")) {

                score = score - 100 / 6;

            } else {


            }

            return score;

        }


        public void submitScore(View view) {
            RadioButton answerOne = (RadioButton) findViewById(R.id.radio_one_correct);
            boolean correctAnswerOne = answerOne.isChecked();

            RadioButton answerTwo = (RadioButton) findViewById(R.id.radio_two_correct);
            boolean correctAnswerTwo = answerTwo.isChecked();

            CheckBox answerThreeA = (CheckBox) findViewById(R.id.checkbox_three_a);
            boolean incorrectAnswerThreeA = answerThreeA.isChecked();

            CheckBox answerThreeB = (CheckBox) findViewById(R.id.checkbox_three_b_correct);
            boolean correctAnswerThreeB = answerThreeB.isChecked();

            CheckBox answerThreeC = (CheckBox) findViewById(R.id.checkbox_three_c_correct);
            boolean correctAnswerThreeC = answerThreeC.isChecked();

            CheckBox answerThreeD = (CheckBox) findViewById(R.id.checkbox_three_d);
            boolean incorrectAnswerThreeD = answerThreeD.isChecked();

            RadioButton answerFour = (RadioButton) findViewById(R.id.radio_four_correct);
            boolean correctAnswerFour = answerFour.isChecked();

            RadioButton answerFive = (RadioButton) findViewById(R.id.radio_five_correct);
            boolean correctAnswerFive = answerFive.isChecked();

            EditText answerSix = (EditText) findViewById(R.id.question_six_edit_text);
            Editable correctAnswerSix = answerSix.getEditableText();

            int finalScore = calculateScore(correctAnswerOne, correctAnswerTwo,
                    incorrectAnswerThreeA, correctAnswerThreeB, correctAnswerThreeC,
                    incorrectAnswerThreeD, correctAnswerFour, correctAnswerFive, correctAnswerSix);

            Toast.makeText(this, "Congratulations! You have a score of " + finalScore + " out of " +
                    "100", Toast.LENGTH_LONG).show();

}

}




Aucun commentaire:

Enregistrer un commentaire