The problem 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.

Breaking it down public static void main ( String [] strings ) {
Scanner input = new Scanner ( System . in );
System . out . print ( "Enter the length of three sides of a triangle: " );
double side1 = input . nextDouble ();
double side2 = input . nextDouble ();
double side3 = input . nextDouble ();
input . close ();
if ( isTriangle ( side1 , side2 , side3 )) {
double perimeter = side1 + side2 + side3 ;
System . out . println ( "The perimeter of your triangle is " + perimeter
+ "." );
} else {
System . out . println ( "Your input is not a valid triangle." );
}
}
private static boolean isTriangle ( double side1 , double side2 , double side3 ) {
boolean isTriangle = (( side1 + side2 > side3 )
&& ( side1 + side3 > side2 ) && ( side3 + side2 > side1 ));
return isTriangle ;
}

Output Compute the perimeter of a triangle posted by Justin Musgrove on 07 April 2016

Tagged: java, java-exercises-beginner, intro-to-java-10th-edition, and ch3

Share on: Facebook Google+