# Is it a right triangle? (Code help)

• 10-07-2008, 06:24 PM
TheApostle
Is it a right triangle? (Code help)
Hi! My task is to create a program that takes the input of three sides of a triangle and displays a message of whether or not it is a right triangle.

Here is what I have so far...
------------------------------
import java.util.Scanner;
public class triangle {

public static void main(String[] args) {

double sidea;
double sideb;
double sidec;

Scanner in = new Scanner(System.in);
System.out.println("Enter the 3 sides of the triangle: ");

sidea = in.nextDouble();
sideb = in.nextDouble();
sidec = in.nextDouble();

//if it is a right triangle
if (((sidea * sidea) + (sideb * sideb)) == (sidec * sidec));
if (((sidea * sidea) + (sidec * sidec)) == (sideb * sideb));
if (((sidec * sidec) + (sideb * sideb)) == (sidea * sidea));

}

}

------------------------------------------------------

I need help making an else statement that says (if all that "if" statement above is NOT true, then it is NOT a right triangle)!

Thanks!!
• 10-07-2008, 07:06 PM
Norm
Code:

```if (A | B | C) {   true; }else{   false; }```
• 10-07-2008, 07:08 PM
TheApostle
Keeps saying that line 29 (true;) and line 31 (false;) are not statements : \
• 10-07-2008, 07:09 PM
TheApostle
sorry those smileys were supposed to be semicolons?- Was I supposed to put that code in underneath my System.out.print?
• 10-07-2008, 07:15 PM
Norm
If you get compile errors, copy FULL text of the error messages here.
I don't know where line 29 and 31 are.

The code I posted was not java. It was just to show you how to use OR with an if statement.
• 10-07-2008, 07:17 PM
ojn
Norm gave you one way to solve this. Read up on how if-else works. I'd do it like this:

Code:

```if (something) {     // code } else if (something else) {     // code } else {     // code }```
Or you can set "answer" to "not a right triangle" and just change it if it is, like you do now. Or you can just a boolean. There are many ways to do this.
• 10-07-2008, 07:19 PM
TheApostle
Ok sorry about being stupid there for a second. I understand what you posted now and I implemented it into my code.
-----------

}else{

System.out.print("Not a right triangle");
-----------

Just one problem is that it is saying that it cannot apply the "OR" (|) operator to a string. (the variable "answer" is a string)
• 10-07-2008, 07:20 PM
CJSLMAN
Check this ...
java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html

CJSL
• 10-07-2008, 08:39 PM
Norm