Hello all, this is my first post in the forums! I have worked through this book (which I love so far, it’s a beautiful tool) and didn’t have any misunderstanding until I came across this challenge.
I created two array lists(correct answers and incorrect answers) through which the user’s answer would be appended to the list in the form of a tally.
private ListCorrect = new ArrayList<>();
private ListIncorrect = new ArrayList<>();
Each correct answer would add a tally to the “correct answers” array and an incorrect answer would add a tally to the “incorrect answers” array, then I created a new method called “check percent” that should be able to compare the two arrays and return the result of the arithmetic.
public int sum_correct; {
int sum = 0;
for (int i : Correct)
sum = sum + i;
}
public int sum_incorrect; {
int sum = 0;
for (int i : Incorrect)
sum = sum + i;
}
public void checkPercent() {
if (sum_correct + sum_incorrect == 6) {
int percent_correct = (sum_correct / 6) * 100;
}
Toast.makeText(this, R.string.score_toast, Toast.LENGTH_LONG).show();
I then created a string resource called “score_toast” that is supposed to show the percentage correct in decimal format:
Score: %1$d percent.
There are no apparent bugs in my code but the proper percentage does not show up in the toast nor does the toast display at the end of the program.
Is this a legitimate way to go about this problem and I’m making some small mistake, or is there a completely different, more efficient way to solve this and I’m missing it completely?
It looks like you intended to create sum_correct and sum_incorrect as methods which read your Correct and Incorrect lists. Instead, you made them fields which are never set. The block of code following each one is an initializer block, which runs whenever an instance of this class is created. The result of this block of code is absolutely nothing, because the local variable sum falls out of scope immediately after you’re done counting the empty list.
When dividing two ints, the result will be an int. As a result, (sum_correct / 6) will always be either 1 (if the player got a perfect score) or 0 (if the player missed at least one question), and multiplying this by 100 will give either 0 or 100. What you probably meant would be written as (sum_correct * 100) / 6.
Toast.makeText does not look for recently declared int variables in the surrounding code, especially ones that have already fallen out of scope. To get the percentage to display in the toast, you would need to use the multiple argument version of getString, and pass the resulting string as the second argument to Toast.makeText.
The toast is set to display whenever checkPercent() is called, whether the user has answered all 6 questions or not. If the toast does not display at the end of the program, that also means you aren’t calling checkPercent() from anywhere.
In addition, some stylistic considerations:
The fields Correct and Incorrect should be lowercase. Only classes and interfaces should begin with a capital letter.
Creating a list just to keep a tally is needlessly complicated. It is much simpler to keep the count in an int and increment it when appropriate.
The number 6 should not be hard coded. Since this is meant to be the number of questions, mQuestionBank.length would better express what you want, and would always be correct even if the code is later updated to have more, fewer, or a variable number of questions.
Thanks for this bro, I was actually stranded but after seeing your code, I now understand how I should go about it.
I tried Arrays but it couldn’t work out in the first place, seems I couldn’t get my head around it .