Sunday 15 February 2015

if statement - Java - program not reading 'else' properly? (logic error) -



if statement - Java - program not reading 'else' properly? (logic error) -

i have been working on programme determining if right triangle can formed 3 user-inputted side lengths. programme uses pythagorean theorem, beingness a2 + b2 = c2. can programme recongnise when triangle cannot built, can't seem recognise when can built.

any suggestions? unsure if error if statements, or simple logic error. (i total beginner @ coding, apologise simplicity of question).

string myinputa = joptionpane.showinputdialog(null, "hello, , welcome 'right' triangle tester.\nthis programme determine if 3 side lengths form right triangle. \nplease input first side length below.","right triangle tester",joptionpane.information_message); string myinputb = joptionpane.showinputdialog(null, "great. please come in sec side below.","right triangle tester", joptionpane.information_message); string myinputc = joptionpane.showinputdialog(null, "please come in lastly side below.","right triangle tester", joptionpane.information_message); double sidea = double.parsedouble(myinputa); double sideb = double.parsedouble(myinputb); double sidec = double.parsedouble(myinputc); if ((sidea * sidea) != ((sideb * sideb) + (sidec * sidec))) { joptionpane.showmessagedialog(null,"i sorry. side lengths not form right triangle.","right triangle tester", joptionpane.error_message); } else if ((sideb * sideb) != (sidec * sidec) + (sidea * sidea)) { joptionpane.showmessagedialog(null,"i sorry. side lengths not form right triangle.","right triangle tester", joptionpane.error_message); } else if ((sidec * sidec) != (sidea * sidea) + (sideb * sideb)) { joptionpane.showmessagedialog(null,"i sorry. side lengths not form right triangle.","right triangle tester", joptionpane.error_message); } else { joptionpane.showmessagedialog(null, "congratulations, side lengths form right triangle.","right triangle tester", joptionpane. information_message); }

your logic wrong. if @ to the lowest degree 1 value^2 value of other two, it's triangle.

in logic, you'll else part if 3 values sum of other 2 (which can happen if 0)

java if-statement logic

No comments:

Post a Comment