/*This program determines whether a triangle can be formed and whether it is a right triangle or not*/
import java.util.Scanner;
public class TriangleFormation {
//Function to calculate for validity
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Enter length of line 1: ");
double a = input.nextDouble();
System.out.println("Enter length of line 2: ");
double b = input.nextDouble();
System.out.println("Enter length of line 3: ");
double c = input.nextDouble();
double a1 = a * a;
double b1 = b * b;
double c1 = c * c;
if (b + c > a || a + c > b || b + a > c) {
System.out.println("Lines can form a triangle");
if (a1 = c1 + b1 || b1 = a1 + c1 || c1 = a1 + b1) {
System.out.println("It is a right triangle");
} else {
System.out.println("It is not a right triangle");
}
} else {
System.out.println("Lines cannot form a triangle");
}
}
}
Converting = to == makes the code work except entering any integers just results in this: Enter length of line 1: Int1 Enter length of line 2: Int2 Enter length of line 3: Int3 Lines can form a triangle It is not a right triangle
Not once does entering any three numbers print the line "Lines cannot form a triangle". I want my code to produce what's shown in the image. Much help would be appreciated. Please and thank you.
It never prints "Lines cannot form a triangle" because the condition you provided,
is always True, so the else statement will never be called.
The Triangle Inequality Theorem states: The sum of the lengths of any two sides of a triangle must be greater than the length of the third side.
So to correctly check for this we need the three conditions you listed above to be satisfied at once, meaning we use AND (&&) instead of OR (||)
Also, a single equal sign (=) assigns your expression to the variable on the left, you want to check equality which is done with the double equal sign, so as you said, replace the = signs in your if statement with ==