50.0k views
2 votes
Write a Java program that prompts the user to enter integer values for the sides of a triangle and then displays the values and the type of triangle they represent. If the user enters values that do not make a valid triangle, or if any values are not greater than zero, your program should display the values entered as well the following error message: The values entered do not make a valid triangle.

User Sachie
by
5.0k points

1 Answer

5 votes

Answer:

import java.util.Scanner;

public class triangle {

public static void main (String [] args) {

int sideOne, sideTwo, sideThree;

Scanner in = new Scanner (System.in);

System.out.println("Enter the first side of the triangle");

sideOne = in.nextInt();

System.out.println("Enter the secon side of the triangle");

sideTwo = in.nextInt();

System.out.println("Enter the third side of the triangle");

sideThree = in.nextInt();

if ( sideOne<=0||sideTwo<=0|| sideThree<=0){

System.out.println(" The Values enter are "+sideOne+ "," +sideThree+ ","+sideThree+ " These values don't make a valid triangle");

}

else if ((sideOne + sideTwo> sideThree) || (sideOne+sideThree > sideTwo) || (sideThree+sideTwo > sideOne))

{

System.out.println ("The triangle is Valid");

}

else {

System.out.println(" The Values enter are "+sideOne+ "," +sideTwo+ ","+sideThree+ " These values don't make a valid triangle");

}

}

Step-by-step explanation:

for a Triangle to be Valid one of the three sides of the triangle must greater than the other two sides. The code above enforces this condition using an if statement in combination with the Or Operator

The following conditions where enforced

side one, side two and side three != 0

Side One + Side Three > Side Two

Side Three + Side Two> Side One

Side Two + Side One > Side Three

User Maarten Faddegon
by
5.6k points