public String displayType(int side1, int side2,int side3)
{
if(( side1+side2 > side3))
if(( side1==side2) && (side2==side3))//tell if
As the final logic (in your case) will always be true, you do not need the final else if
simply
return ("Scalene Triangle.");
But what will happen if the below if false?
if(( side1+side2 > side3))
You need to return something.
The reason is that, What happens all of your conditions failed to execute ??
Possible solutions :
You need to provide an else block there.When you provide a else
block, then it is sure that always there is a chance to return the
value, either in if
or in else
.
A default return statement without regarding if-else
, where the
returning value should be decide in blocks.
Try this
public String displayType(int side1, int side2,int side3)
{
if(( side1+side2 > side3)){
if(( side1==side2) && (side2==side3))//tell if equalateral
{
return "Equalateral Triangle.";
}
else if (( side1==side2) & (side2 != side3) || (side1 == side3) & (side3 != side1))//tells if isosceles
{
return "Isosceles Triangle.";
}
else if ((side1 != side2) & (side2 != side3))//tells if scalene
{
return "Scalene Triangle.";
}
}
else{
return "Not a triangle";
}
}
Compiler is saying that You have to return something,because if all your IF conditions fail then what will it do?
you can do like this:
public String displayType(int side1, int side2,int side3)
{
if(( side1+side2 > side3))
{
if(( side1==side2) && (side2==side3))//tell if equalateral
{
return ("Equalateral Triangle.");
}
else if (( side1==side2) & (side2 != side3) || (side1 == side3) & (side3 != side1))//tells if isosceles
{
return ("Isosceles Triangle.");
}
else if ((side1 != side2) & (side2 != side3))//tells if scalene
{
return ("Scalene Triangle.");
}
return ("It is not a triangle");
} // missing return statement here error