【问题标题】:How to handle division by zero exception如何处理除以零异常
【发布时间】:2017-02-28 03:33:00
【问题描述】:

我使用 php 制作了一个简单的计算器,我想处理除以零的异常,我是新手,需要帮助如何做到这一点,如果有人可以提供帮助:- 下面是我的代码

<?php

$result = 0;
class calculator
{
    var $a;
    var $b;
    function checkoperation($operator)
    {
        switch ($operator) {

            case 'divide':
                return $this->a / $this->b;
                break;

            case 'multiply':
                return $this->a * $this->b;
                break;

            case 'add':
                return $this->a + $this->b;
                break;

            case 'subtract':
                return $this->a - $this->b;
                break;
        }
    }
    function getresult($a, $b, $c)
    {
        $this->a = $a;
        $this->b = $b;
        return $this->checkoperation($c);
    }
}
$cal = new calculator();
if (isset($_POST['calculate_btn'])) {
    $first_num  = $_POST['first_num_txtbox'];
    $second_num = $_POST['second_num_txtbox'];
    $operation  = $_POST['operation_slctbox'];
    $result     = $cal->getresult($first_num, $second_num, $operation);
    echo "The result is: " . $result;
}
?>

【问题讨论】:

    标签: php exception-handling divide-by-zero


    【解决方案1】:

    在 devide 情况下,必须在 make devide 运算符之前检查 $this->b 值:

    case 'divide':
        return ($this->b == 0) ? 'Infinitive' : $this->a / $this->b;
        break;
    

    希望对你有帮助!

    【讨论】:

    • 如果数字 1 是 0 怎么办?
    • $this没有问题->a为0,结果为0
    【解决方案2】:

    把 if 语句放在最后。我的意思是:

    if ($second_num==0)
    {echo "<h1>ERROR:CANT BE DIVIDED</h1>"}
    else
    {$result = $cal->getresult($first_num, $second_num, $operation);
    echo "The result is: " . $result;}
    

    【讨论】:

    • 如果第一个数字是0怎么办?
    【解决方案3】:

    这将是一个选项,检查后这只是一个选项,它会检查两个数字不是0

    if(isset($_POST['calculate_btn']))
    {   
        if($_POST['operation_slctbox'] == "divide")
        {
            if($_POST['first_num_txtbox'] !== "0" || $_POST['second_num_txtbox'] !== "0")
            {
                //do code if either are 0 and divide is selected or return with error cannot divide by 0
            }
            else
            {
               $result = $cal->getresult($first_num, $second_num, $operation);
            }
        }
        else
        {
             $result = $cal->getresult($first_num, $second_num, $operation);
        }
        echo "The result is: " . $result;
    }
    

    【讨论】:

      【解决方案4】:

      你可以使用 try catch 处理异常

             case 'divide':
                   try {
                      if(!$this->b){
                          throw new Exception('Division by zero .');
                      }
                          return $this->a / $this->b;
                  } catch (Exception $e) {
                      return $e->getMessage();
                  }
                  break;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-01
        相关资源
        最近更新 更多