0% found this document useful (0 votes)
59 views3 pages

Randomly Generating Equation and Answer: Generatenumbers Oncreateview

The document is a Stack Overflow post asking how to randomly generate math equations along with correct and incorrect answers. The code provided was generating inconsistent equations due to numbers being re-generated within loops. Suggested solutions were to add a return statement after regenerating numbers for divisions, and to determine the operator after generating numbers to avoid multiple generations.

Uploaded by

redchaoz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views3 pages

Randomly Generating Equation and Answer: Generatenumbers Oncreateview

The document is a Stack Overflow post asking how to randomly generate math equations along with correct and incorrect answers. The code provided was generating inconsistent equations due to numbers being re-generated within loops. Suggested solutions were to add a return statement after regenerating numbers for divisions, and to determine the operator after generating numbers to avoid multiple generations.

Uploaded by

redchaoz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

java - Randomly generating equation and answer - Stack Overflow

http://stackoverflow.com/questions/29918425/randomly-generating-equ...

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. The
incorrect 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 generateNumbers() in my onCreateView method
public voidgenerateNumbers() {
//randomlygenerate2numbersandanoperator
number1= (int) (Math.random() * 10) + 1;
number2= (int) (Math.random() * 10) + 1;
operator = (int) (Math.random() * 4) + 1;
//50%chancewhetherthedisplayedanswerwillberightorwrong
rightOrWrong= (int) (Math.random() * 2) + 1;
//calculatetheoffsetofdisplayedanswerforawrongequation(Error)
error= (int) (Math.random() * 4) + 1;
generateEquation();
}
public voidgenerateEquation() {
StringBuilderequation= new StringBuilder();
//appendthefirstnumber
equation.append(number1);
//generate/appendtheoperatorandcalculatetherealanswer
if (operator == 1) {
equation.append("+");
actualAnswer=number1+number2;
} else if (operator == 2) {
equation.append("");
actualAnswer=number1number2;
} 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("/");
}
//appendthesecondnumberandtheequalssign
equation.append(number2+ "=");
//wewilldisplaythecorrectanswerfortheequation
if (rightOrWrong== 1) {
displayedAnswer=actualAnswer;
equation.append(displayedAnswer);
}
//wewilldisplayanincorrectanswerfortheequation
//needtocalculateerror(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;
}
//appendthedisplayedanswerwitherror
equation.append(displayedAnswer);
}
questionNumber.setText("Youhaveanswered" +count+ "outof20questions");
finalEquation.setText(equation.toString());
}
java

android

random

equation

asked Apr 28 at 11:39

1 of 3

6/9/2015 3:18 PM

java - Randomly generating equation and answer - Stack Overflow

http://stackoverflow.com/questions/29918425/randomly-generating-equ...

Luke
17

There's some weird things in your code. You generate numbers, then generate equation. Then while
generating your equation you generate new numbers if their not suiteable for division, so the generate
numbers function is called again. This in turn again calls the generate equation function, but when this
finishes, 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't
figure it out. The marked answer solved it Luke Apr 28 at 11:58

2 Answers

I think you need to put a return statement after the call to generateNumbers in
} 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 Hampl
22.6k

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 actually
displaying 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 voidgenerateNumbers() {
number1= (int) (Math.random() * 10) + 1;
number2= (int) (Math.random() * 10) + 1;
//don'tgetoperatorhere
//operator=(int)(Math.random()*4)+1;
rightOrWrong= (int) (Math.random() * 2) + 1;
error= (int) (Math.random() * 4) + 1;
//don'tgenerateequationhere,start(intheViewwithgenerateEquationinsteadof
generatenumbers
//generateEquation();
}
public voidgenerateEquation() {
StringBuilderequation= new StringBuilder();
generateNumbers();
//determineoperatorhere
operator = (int) (Math.random() * 4) + 1;

equation.append(number1);
if (operator == 1) {
equation.append("+");
actualAnswer=number1+number2;
} else if (operator == 2) {
equation.append("");
actualAnswer=number1number2;
} else if (operator == 3) {
equation.append("x");
actualAnswer=number1*number2;
} else if (operator == 4) {
equation.append("/");
//generatenewnumbersiftheyarenotsuiteable
while((number1%number2!=0) && (number1<number2))
{
generateNumbers();
}
actualAnswer=number1/number2;
}
......

2 of 3

6/9/2015 3:18 PM

java - Randomly generating equation and answer - Stack Overflow

http://stackoverflow.com/questions/29918425/randomly-generating-equ...

answered Apr 28 at 12:52


JohannisK
267

Could probably be better, but I think this will solve the problem. JohannisK Apr 28 at 12:54

3 of 3

6/9/2015 3:18 PM

You might also like