java - Else condition seemingly fails to execute -
i creating simple mathematics practice program, running serious issue else conditions. when conditions should trigger else block run, if statement runs instead, ie if program prompts user answer question 7+6, if input 5 still procede if had input proper answer. have combed through each iteration of if else series (this 1 of 5) last few hours, cannot figure occurring prevent else segment running.
correctrange / correctrange2 random generated int values (currently 7 & 6 respectively d1 & d2 same values doubles human error parameter when input1 = q program supposed end edit: here updated version, error persists
//addition boolean addition = false; while(input1 == 'a'|| input1 == 'a') { system.out.println("what solution problem " + correctrange + " + " + correctrange2 ); double input2 = userinput.nextdouble(); if(input2 <= (d1 + d2) + human || input2 >= (d1 + d2) - human) {system.out.println("that correct!"); system.out.println("what practice next?"); addition = true; } while (!addition) {system.out.println("the correct solution " + correctrange + correctrange2); input1 = 'q'; } input1 = userinput.next().charat(0); }
every time call userinput.nextdouble()
reads new double. if call twice expect new double.
if(userinput.nextdouble() <= (d1 + d2) + human || userinput.nextdouble() >= (d1 + d2) - human)
this read 1 or 2 double if first condition false.
most intended
double input = userinput.nextdouble(); if(input <= (d1 + d2) + human || input >= (d1 + d2) - human)
or can write
if(math.abs(input - (d1 + d2)) <= human)
note: =
assignment , ==
comparison so
while (addition = false)
is false
intended be
while (addition == false)
or better
while (!addition)
Comments
Post a Comment