-3

Trying to find the type of the triangle when the user enters valueother than 0,then proceed to the area by using Heron formula.If entered 0,The program should terminate by saying it is not valid,not proceed to the area.

import java.util.Scanner;
public class triangle
{
public static void main(String args[])
{
    Scanner input = new Scanner(System.in);

    System.out.print("Enter the length of the first side: ");
    int firstSide = input.nextInt();

    System.out.print("Enter the length of the second side: ");
    int secSide = input.nextInt();

    System.out.print("Enter the length of the third side: ");
    int thirdSide = input.nextInt();

   if(firstSide==0||secSide==0||thirdSide==0)
   System.out.println("This is not a valid triangle");
   else
   if(firstSide==secSide&&secSide==thirdSide&&thirdSide==firstSide)
   System.out.println("This is an equilateral triangle");

   else 
   if((firstSide==secSide)||(secSide==thirdSide)||                 (thirdSide==firstSide))
        System.out.println("This is an isoceles triangle");
   else 
   if(firstSide!=secSide&&secSide!=thirdSide&&thirdSide!=firstSide)
        System.out.println("This is a scalene triangle");

   double s,x;
   s=(firstSide+secSide+thirdSide)/2.0;
   x=(s * (s-firstSide) * (s-secSide) * (s-thirdSide));

   double Area = Math.sqrt(x);

   System.out.print("The area is: " + Area);



 }
 }

sample run
Output:
Enter the length of the first side: 0;
Enter the length of the second side: 6;
Enter the length of the third side: 1029;
This is not a valid triangle.;



 Enter the length of the first side: 2 ;
Enter the length of the second side: 3;
Enter the length of the third side: 4;
This is a scalene triangle.;
The area is: 2.9;

2 Answers2

0

I'm not sure why your if else statements are on separate lines, but try adding a simple return statement in your first if so that it doesn't proceed to execute the calculation code:

if(firstSide==0||secSide==0||thirdSide==0)
{
   System.out.println("This is not a valid triangle");
   return;
}
Peter
  • 498
  • 3
  • 15
  • I am not supposed to use system.exit or return statement to terminate. It should automatically not execute the are when the value is 0 – Shifa Farheen Feb 15 '17 at 02:32
  • 1
    @ShifaFarheen Well those are silly rules... I guess just surround the area calculation code with the inverse of the `if` you used earlier: `if(firstSide!=0 && secSide!=0 && thirdSide!=0)` – Peter Feb 15 '17 at 02:38
  • If each side is not equal to zero, only then should you display the area. – Peter Feb 15 '17 at 02:40
0

it would be better to use curly braces so that you can see where your nested if are

if(firstSide==0||secSide==0||thirdSide==0)
{
   System.out.println("This is not a valid triangle");
}
else {
   if(firstSide==secSide&&secSide==thirdSide&&thirdSide==firstSide)
   {
     System.out.println("This is an equilateral triangle");
   }
   else
   { 
     if((firstSide==secSide)||(secSide==thirdSide)|| (thirdSide==firstSide))
        System.out.println("This is an isoceles triangle");
     else // is this if needed?
        if(firstSide!=secSide&&secSide!=thirdSide&&thirdSide!=firstSide)
           System.out.println("This is a scalene triangle");
   }

   double s,x;
   s=(firstSide+secSide+thirdSide)/2.0;
   x=(s * (s-firstSide) * (s-secSide) * (s-thirdSide));

   double Area = Math.sqrt(x);

   System.out.print("The area is: " + Area);
}

edit but even better would be to return early

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64