|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I'm new to PHP, and I'm stuck. I created the below class, and the switch
statement in Calculator.calculate() doesn't find a successful match for any of the case values. I've checked with echo's, and the values are right (no extra whitespace). What am I not doing right? Thanks, Paul class Calculator { var $operand1; var $operand2; var $operator; var $result = "no result"; function Calculator ($o1, $o2, $op) { $this->operand1 = $o1; $this->operand2 = $o2; $this->operator = $op; } // Calculator function calculate () { switch ($operator) { case "+": $result = $op1 + $op2; break; case "-": $result = $op1 - $op2; break; case "*": $result = $op1 * $op2; break; case "/": $result = $op1 / $op2; break; case "div": $result = (int)$op1 / (int)$op2; break; case "mod": $result = $op1 % $op2; break; default: echo "calculate: operator not found<br>"; break; } // switch return $result; } // calculate } // class Calculator |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Sep 17, 10:49 am, "Paul Hunter" <baseball...@comcast.net> wrote:
> I'm new to PHP, and I'm stuck. I created the below class, and the switch > statement in Calculator.calculate() doesn't find a successful match for any > of the case values. I've checked with echo's, and the values are right (no > extra whitespace). What am I not doing right? > > Thanks, > Paul > > class Calculator { > > var $operand1; > var $operand2; > var $operator; > var $result = "no result"; > > function Calculator ($o1, $o2, $op) { > $this->operand1 = $o1; > $this->operand2 = $o2; > $this->operator = $op; > } // Calculator > > function calculate () { > switch ($operator) { > case "+": > $result = $op1 + $op2; > break; > case "-": > $result = $op1 - $op2; > break; > case "*": > $result = $op1 * $op2; > break; > case "/": > $result = $op1 / $op2; > break; > case "div": > $result = (int)$op1 / (int)$op2; > break; > case "mod": > $result = $op1 % $op2; > break; > default: > echo "calculate: operator not found<br>"; > break; > } // switch > > return $result; > } // calculate > > } // class Calculator Inside calculate() don't you really want to be using $this->operand1, $this->operand2, and $this->operator? |
|
![]() |
| Outils de la discussion | |
|
|