0

I have the following If statement:

private boolean NextLevel(int intCorrectAnswers, int intNumberOfQuestionsAsked) {
    if (intNumberOfQuestionsAsked >= 20 && ((intCorrectAnswers / intNumberOfQuestionsAsked) >= .8)){
        return true;
    } else {
        return false;
    }
}

What I want is if the number of questions asked is at least 20 and the percentage correct is at least 80% then return true.

Stepping through the code, I am answering at least 46 questions and getting all but 1 correct and it always returns false. What am I missing?

Ziem
  • 6,579
  • 8
  • 53
  • 86
David.Warwick
  • 620
  • 1
  • 9
  • 28

1 Answers1

0

You are performing an integer division which will not return a floating or decimal value that is why your result is always false. Cast your variable to double or float to get your desired result.

double dResult = ((double)intCorrectAnswers / (double)intNumberOfQuestionsAsked);
if (intNumberOfQuestionsAsked >= 20 && (dResult >= 0.8)) {
    return true;
} else {
    return false;
}
Ziem
  • 6,579
  • 8
  • 53
  • 86
Christian Abella
  • 5,747
  • 2
  • 30
  • 42