Joined
·
1,290 Posts
I have to write a program that will take 3 numbers and tell me if I have a right triangle, a triangle but not a right triangle, or no triangle at all. I have a program (see code below) that works, but it seems incredibly bloated for what I want to do. Is there a simpler way to write a program that will do what I want?
Code:
Code:
Code:
import java.util.Scanner;
/**
* Compares 3 numbers and determines what kind,
*if any, triangle can be formed.
* @
* 09/19/2010
*/
public class TestTriangle {
public static void main (String [] args)
{
Scanner in = new Scanner (System.in);
System.out.println("Enter 3 numbers");
int num1 = in.nextInt();
int num2 = in.nextInt();
int num3 = in.nextInt();
if ((num1^2 + num2^2) == (num3^2))
System.out.printf("%d, %d, and %d: Right Triangle %n", num1, num2, num3);
else if ((num2^2 +num3^2)== (num1^2))
System.out.printf("%d, %d, and %d: Right Triangle %n", num1, num2, num3);
else if ((num3^2 + num1^2) == (num2^2))
System.out.printf("%d, %d, and %d: Right Triangle %n", num1, num2, num3);
else
if ((num1 + num2 <= num3))
System.out.printf("%d, %d, and %d do not form" +
" a triangle %n", num1, num2, num3);
else if ((num2 + num3 <= num1))
System.out.printf("%d, %d, and %d do not form" +
" a triangle %n", num1, num2, num3);
else if (num3 + num1 <= num2)
System.out.printf("%d, %d, and %d do not form" +
" a triangle %n", num1, num2, num3);
else
if ((num1 + num2 > num3))
System.out.printf("%d, %d, and %d: Triangle, but not a Right Triangle %n", num1, num2, num3);
else if ((num1 + num3 > num2))
System.out.printf("%d, %d, and %d: Triangle, but not a Right Triangle %n", num1, num2, num3);
else if ((num2 + num3 > num1))
System.out.printf("%d, %d, and %d: Triangle, but not a Right Triangle %n", num1, num2, num3);
}
}