Missing return statement making triangle program

后端 未结 4 552
花落未央
花落未央 2021-01-27 09:24
public String displayType(int side1, int side2,int side3)
    {
        if(( side1+side2 > side3))

            if(( side1==side2) && (side2==side3))//tell if         


        
相关标签:
4条回答
  • 2021-01-27 09:34

    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.

    0 讨论(0)
  • 2021-01-27 09:41

    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.

    0 讨论(0)
  • 2021-01-27 09:43

    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";
          }
      }
    
    0 讨论(0)
  • 2021-01-27 09:44

    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
    
    0 讨论(0)
提交回复
热议问题