Stack Overflow
description
Transcript of Stack Overflow
sign up log in tour help stack overflow careers
×Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.
Randomly generating equation and answer
I am trying to randomly generate an equation that also has a 50% chance of being wrong and displaying that incorrect answer. Theincorrect answer should have an error of either -2, -1, +1, or +2.
Sometimes my code prints division equations like this(I can't post images): 2 / 10 = 13 1 / 5 = 43 etc.
I can't figure out why the equation is displaying a mix of numbers that are not checked together?
(It starts with a call to in my methodgenerateNumbers() onCreateView
public void generateNumbers() {
//randomly generate 2 numbers and an operator
number1 = (int) (Math.random() * 10) + 1;
number2 = (int) (Math.random() * 10) + 1;
operator = (int) (Math.random() * 4) + 1;
//50% chance whether the displayed answer will be right or wrong
rightOrWrong = (int) (Math.random() * 2) + 1;
//calculate the offset of displayed answer for a wrong equation (Error)
error = (int) (Math.random() * 4) + 1;
generateEquation();
}
public void generateEquation() {
StringBuilder equation = new StringBuilder();
//append the first number
equation.append(number1);
//generate/append the operator and calculate the real answer
if (operator == 1) {
equation.append(" + ");
actualAnswer = number1 + number2;
} else if (operator == 2) {
equation.append(" ‐ ");
actualAnswer = number1 ‐ number2;
} else if (operator == 3) {
equation.append(" x ");
actualAnswer = number1 * number2;
} else if (operator == 4) {
if ((number1%number2==0) && (number1>number2)) {
actualAnswer = number1 / number2;
} else {
generateNumbers();
}
equation.append(" / ");
}
//append the second number and the equals sign
equation.append(number2 + " = ");
//we will display the correct answer for the equation
if (rightOrWrong == 1) {
displayedAnswer = actualAnswer;
equation.append(displayedAnswer);
}
//we will display an incorrect answer for the equation
//need to calculate error (‐2, ‐1, +1, +2)
else {
if (error == 1) {
displayedAnswer = actualAnswer ‐ 1;
} else if (error == 2) {
displayedAnswer = actualAnswer ‐ 2;
}else if (error == 3) {
displayedAnswer = actualAnswer + 1;
}else {
displayedAnswer = actualAnswer + 2;
}
//append the displayed answer with error
equation.append(displayedAnswer);
}
questionNumber.setText("You have answered " + count + " out of 20 questions");
finalEquation.setText(equation.toString());
}
java android random equation
asked Apr 28 at 11:39
java - Randomly generating equation and answer - Stack Overflow http://stackoverflow.com/questions/29918425/randomly-generating-equ...
1 of 3 6/9/2015 3:18 PM
Luke17 5
–
There's some weird things in your code. You generate numbers, then generate equation. Then whilegenerating your equation you generate new numbers if their not suiteable for division, so the generatenumbers function is called again. This in turn again calls the generate equation function, but when thisfinishes, the first call to generate equation continues. JohannisK Apr 28 at 11:45
– Yeah I this was what I was thinking when I was trying to solve it, but I went way too in depth and couldn'tfigure it out. The marked answer solved it Luke Apr 28 at 11:58
2 Answers
I think you need to put a statement after the call to inreturn generateNumbers
} else if (operator == 4) {
if ((number1%number2==0) && (number1>number2)) {
actualAnswer = number1 / number2;
} else {
generateNumbers();
}
equation.append(" / ");
}
since that will restart the whole process, rather than continuing with more numbers.
answered Apr 28 at 11:45
Jakub Hampl22.6k 6 45 81
– Great this works thank you. Don't know how I didn't think of that. Only now I realised the chance of it actuallydisplaying a division equation is pretty low, but it still works. Luke Apr 28 at 11:55
Change your code to loose the low chance division problem:
public void generateNumbers() {
number1 = (int) (Math.random() * 10) + 1;
number2 = (int) (Math.random() * 10) + 1;
//don't get operator here
//operator = (int) (Math.random() * 4) + 1;
rightOrWrong = (int) (Math.random() * 2) + 1;
error = (int) (Math.random() * 4) + 1;
//don't generate equation here, start (in the View with generateEquation instead of
generate numbers
//generateEquation();
}
public void generateEquation() {
StringBuilder equation = new StringBuilder();
generateNumbers();
//determine operator here
operator = (int) (Math.random() * 4) + 1;
equation.append(number1);
if (operator == 1) {
equation.append(" + ");
actualAnswer = number1 + number2;
} else if (operator == 2) {
equation.append(" ‐ ");
actualAnswer = number1 ‐ number2;
} else if (operator == 3) {
equation.append(" x ");
actualAnswer = number1 * number2;
} else if (operator == 4) {
equation.append(" / ");
// generate new numbers if they are not suiteable
while((number1%number2!=0) && (number1<number2))
{
generateNumbers();
}
actualAnswer = number1 / number2;
}
......
java - Randomly generating equation and answer - Stack Overflow http://stackoverflow.com/questions/29918425/randomly-generating-equ...
2 of 3 6/9/2015 3:18 PM
answered Apr 28 at 12:52
JohannisK267 7
– Could probably be better, but I think this will solve the problem. JohannisK Apr 28 at 12:54
java - Randomly generating equation and answer - Stack Overflow http://stackoverflow.com/questions/29918425/randomly-generating-equ...
3 of 3 6/9/2015 3:18 PM