2014-03-12 53 views
0

即時通訊想要檢查輸入的三角形是否是等腰,等號等等。第一個if語句有效,但是一旦我介紹第二個,它表示意外的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>"; 
    } 
} 

有什麼想法嗎?

+1

您在'echo'語句後忘記了第一個右括號'}'。 – jeroen

回答

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

你有一個語法錯誤:

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

需要一個支柱}

 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

我會建議略有不同,更可讀的辦法:

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"; 
    } 
}