import java.util.Scanner; /** * Program to classify triangles. You enter the lengths of the sides, and * the program tells you whether the triangle is equilateral, scalene, or isosceles. * For a refresher on what these terms mean, see * Wikipedia. * *
The program also detects if the specified triangle is impossible under a * Euclidean geometry. * *
Idea for extension, if you want to try something: make the program detect * right triangles. Note that a right triangle can be either isosceles or scalene! * * @author pollen */ public class TriangleClassification { public static void main(String[] args) { Scanner in = new Scanner(System.in); // Collect the lengths of the three ties. Tell the user to separate // the values by spaces so that in.nextDouble can distinguish them. System.out.println("Enter the lengths of the three sides, separated by spaces: "); double a = in.nextDouble(); double b = in.nextDouble(); double c = in.nextDouble(); // First check if it is possible for a triangle to be formed of these three // sides. if ((a + b <= c) || (b + c <= a) || (a + c <= b)) { System.out.println("No such triangle exists, at least in Euclidean space."); } else { // Perform basic equality tests once and for all. boolean aEqualsB = (a == b); boolean bEqualsC = (b == c); boolean aEqualsC = (a == c); if (aEqualsB && bEqualsC && aEqualsC) { System.out.println("This triangle is equilateral."); } else if (aEqualsB || bEqualsC || aEqualsC) { System.out.println("This triangle is isosceles."); } else { System.out.println("This triangle is scalene."); } } } }