Challenge:Who have solutions to give a percentage score


#1

Challenge:When the user has answered all the questions, he displays a toast message and gives the percentage form


#2

Hi,evrybody:
I am actually working through this great book and am solving now the second challenge exercises of chapter 3,and the title is: when the user has finished all, he displays a toast message and gives a percentage score .
But i have no idea about this,as toast messages should not be String type?


#3

You can show toasts with strings too. You can use either a resource ID or a string value.

This version of makeText can operate off of a String value: https://developer.android.com/reference/android/widget/Toast.html#makeText(android.content.Context, java.lang.CharSequence, int).


#4

Thanks,I’ll try.It’s really a good book to improve myself.


#7

I can’t open this link.
can you share code in this net?
Thank you !


#8

Here is my solution for the two challenges in chapter 3. The code is marked.

package com.bignerdranch.android.geoquiz;


// challenge 3.1
import java.util.ArrayList;

public class QuizActivity extends AppCompatActivity {

private static final String TAG = "QuizActivity";
private static final String KEY_INDEX = "index";

private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private TextView mQuestionTextView;
// challenge 3.1
private ArrayList<Integer> mQuestionAsked = new ArrayList<Integer>(6);
// challenge 3.2
private int mTrueAnswer = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, “onCreate(Bundle) called”);
setContentView(R.layout.activity_quiz);

    if (savedInstanceState != null) {
        mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
        // challenge 3.1
        mQuestionAsked = savedInstanceState.getIntegerArrayList("myArray");
    }

    mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
    mQuestionTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
            updateQuestion();
        }
    });

private void updateQuestion() {
    int question = mQuestionBank[mCurrentIndex].getTextResId();
    mQuestionTextView.setText(question);

    /* challenge 3.1................*/
    mFalseButton.setVisibility(View.VISIBLE);
    mTrueButton.setVisibility(View.VISIBLE);

    for (Integer i = 0; i < mQuestionAsked.size(); i++) {
        if (mCurrentIndex == mQuestionAsked.get(i)) {
            mFalseButton.setVisibility(View.INVISIBLE);
            mTrueButton.setVisibility(View.INVISIBLE);
        }
    }
    /*...............................*/
    // challenge 3.2
    int resultResId = (mTrueAnswer*100) / 6;
    if (mQuestionAsked.size() > 5) {
        Toast.makeText(this, Integer.toString(resultResId)+"% correct answers", Toast.LENGTH_LONG)
                .show();
    }
}

 @Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    Log.i(TAG, "onSaveInstanceState");
    savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
    // challenge 3.1
    savedInstanceState.putIntegerArrayList("myArray", mQuestionAsked);
}

private void checkAnswer(boolean userPressedTrue) {
    boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();

    // challenge 3.1
    mQuestionAsked.add(mCurrentIndex);
    mFalseButton.setVisibility(View.INVISIBLE);
    mTrueButton.setVisibility(View.INVISIBLE);
    /*...............................*/

    int messageResId = 0;

    if (userPressedTrue == answerIsTrue) {
        messageResId = R.string.correct_toast;
        // challenge 3.2
        mTrueAnswer = mTrueAnswer + 1;
    } else {
        messageResId = R.string.incorrect_toast;
    }
    Toast.makeText(this, messageResId, Toast.LENGTH_SHORT)
            .show();
}

}


#9

My solution:

...
private int correctAnswers = 0;
...

private void checkAnswer(boolean userPressedTrue) {
    questionsAnswered[currentIndex] = true;
    setButtonsEnabled(false);

    boolean answerIsTrue = questionBank[currentIndex].isAnswerTrue();

    int messageResId;

    if (userPressedTrue == answerIsTrue) {
        messageResId = R.string.toast_correct;
        correctAnswers++;
    } else {
        messageResId = R.string.toast_incorrect;
    }

    Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
    calculateScore();
}

private void calculateScore() {
    // check all questions answered
    for (boolean answered : questionsAnswered) {
        if (!answered) return;
    }
    // all answered, show message
    int score = correctAnswers * 100 / questionBank.length;
    String message = getString(R.string.toast_score, score);
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}

And my strings.xml with placeholder

<resources>
    ...
    <string name="toast_score">You have %1$d%% correct answers!</string>
    ...
</resources>