2014-03-12 2 views
0

im은 입력 된 삼각형이 이등변 삼각형인지 등등인지 확인하려고합니다. 첫 번째 if 문은 작동하지만 예기치 않은 t_else를 소개하면 예기치 않은 t_else가됩니다. ...함수의 여러 IF 문 - OOP

public function typeOfTriangle() 
{ 
     if ($this->sideOneLength == $this->sideTwoLength && $this->sideTwoLength == $this->baseLength) { 
      echo "<h1>Equilateral Triangle</h1>"; 
     else if ($this->sideOneLength == $this->sideTwoLength OR $this->sideOneLength == $this->baseLength OR $this->sideTwoLength == $this->baseLength) { 
      echo "<h1>Isosceles Triangle</h1>"; 
    } 
} 

아이디어가 있습니까? 아마도 이것은 짝수

public function getSides() 
{ 
    return array($this->sideOneLength, $this->sideTwoLength, $this->sideThreeLength); 
} 

public function typeOfTriangle() 
{ 
    $uniqueSides = count(array_unique($this->getSides())); 

    if ($uniqueSides == 1) { 
     return "Equilateral"; 
    } elseif ($uniqueSides == 2) { 
     return "Isosceles"; 
    } else { 
     return "Normal triangle"; 
    } 
} 
+1

'echo' 문 뒤에 첫 번째 닫기 중괄호를 잊어 버렸습니다. – jeroen

답변

0

:

public function typeOfTriangle() 
{ 
     if ($this->sideOneLength == $this->sideTwoLength && $this->sideTwoLength == $this->baseLength) { 
      echo "<h1>Equilateral Triangle</h1>"; 
     }else if ($this->sideOneLength == $this->sideTwoLength OR $this->sideOneLength == $this->baseLength OR $this->sideTwoLength == $this->baseLength) { 
      echo "<h1>Isosceles Triangle</h1>"; 
     } 
} 
0

당신은 구문 오류가 보다 나은? (Vlad Preda의 예를 기반으로 :

public function getSides() 
{ 
    return array($this->sideOneLength, $this->sideTwoLength, $this->sideThreeLength); 
} 

public function typeOfTriangle() 
{ 
    $uniqueSides = count(array_unique($this->getSides())); 

    switch ($uniqueSides) { 
     case 1: 
     return "Equilateral"; 
     break; 

     case 2: 
     return "Isosceles"; 
     break; 

     default: 
     return "Normal triangle"; 
     break; 
    } 

} 
0

}

 if ($this->sideOneLength == $this->sideTwoLength && $this->sideTwoLength == $this->baseLength) { 
      echo "<h1>Equilateral Triangle</h1>"; 
     } 
     else if ($this->sideOneLength == $this->sideTwoLength OR $this->sideOneLength == $this->baseLength OR $this->sideTwoLength == $this->baseLength) { 
      echo "<h1>Isosceles Triangle</h1>"; 
     } 
0

내가 약간 다른, 더 읽기 접근 방식을 제안 중괄호가 필요합니다