mardi 3 mars 2015

Generating random simple math division problems

I'm making a simple Android app for my daughter so she can do math problems. I found it fairly easy to generate addition, subtraction and multiplication questions at random, however division is more tricky.


I don't want to get into decimals, so I have to check each is divisible without remainder. This is what I am working with...



public void genDivQuestion() {
text = (TextView) findViewById(R.id.textView1);
text.setText(divisible());
answer = first / second;
}

public String divisible() {
first = RandomNumGen.getRandIntBetween(2, 12);
second = RandomNumGen.getRandIntBetween(2, 12);
if (first % second == 0) {
return first + " \u00F7 " + second + " = ?";
} else {
return "0";
}
}


I'm not sure what to do at the line "return "0";". If I try calling divisible() to try again, the app just crashes.





Aucun commentaire:

Enregistrer un commentaire