Java Boolean Logical Operators triangle side

Question

We would like to write a program that reads three edges for a triangle.

Compute the perimeter if the input is valid.

Otherwise, display that the input is invalid.

The input is valid if the sum of every pair of two edges is greater than the remaining edge.

import java.util.Scanner;

public class Main {

    public static void main(String[] strings) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter 3 edge lengths of a triangle: ");
        double side1 = input.nextDouble();
        double side2 = input.nextDouble();
        double side3 = input.nextDouble();

        //your code here
    }//w ww.  j a  v  a2 s.  co m
}



import java.util.Scanner;

public class Main {

    public static void main(String[] strings) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter 3 edge lengths of a triangle: ");
        double side1 = input.nextDouble();
        double side2 = input.nextDouble();
        double side3 = input.nextDouble();

        boolean isTriangle = ((side1 + side2 > side3) &&
                (side1 + side3 > side2) &&
                (side3 + side2 > side1));

        if (isTriangle) {
            double perimeter = side1 + side2 + side3;
            System.out.println("You entered a real triangle with the perimeter of " + perimeter + ".");
        } else {
            System.out.println("Your input is not a valid triangle.");
        }
    }
}



PreviousNext

Related