Friday, June 20, 2014

Java Exercise Nr. 34 (Selections)

Write a program that reads three edges for a triangle and computes 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.

Solution:

public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      double side1, side2, side3;
      double perimeter;

      System.out.print("Enter the three sides of the rectangle:");
      side1 = s.nextDouble();
      side2 = s.nextDouble();
      side3 = s.nextDouble();

      if((side1 + side2 >= side3) && (side1 + side3 >= side2) && (side2 + side3 >= side1)) {
perimeter = side1 + side2 + side3;
System.out.print("The perimeter of the triangle is " + perimeter + ".");
      } else {
System.out.print("Invalid values entered.");
      }
}

No comments:

Post a Comment

Author