• 给出2个数和一个运算符号,用函数求结果


    //给出2个数和一个运算符号 求结果
    $a = 15;//第一个数
    $b = 20;//第二个数字
    $c='/';//运算符
    $res = 0;//2个数字的结果
    if($c == '+'){
        $res = $a+$b;
    }else if($c == '-'){
        $res  = $a-$b;
    }else if($c == '*'){
        $res = $a*$b;
    }else if($c == '/'){
        $res = $a/$b;
    }
    echo $a.$c.$b.'='.$res;
    echo '<br/>';

    //因为有明确的数字,不是范围所以可以用switch改造
    $a = 10; //第一个数
    $b = 20; //第二个数
    $c = '/'; //运算符号
    $res = 0;  //结果
    switch($c){
        case '+':
        $res = $a+$b;
        break;

        case '-':
        $res = $a-$b;
        break;

        case '*':
        $res = $a*$b;
        break;

        case '/':
        $res = $a/$b;
        break;

        default://默认的 其它的情况走这路代码
        echo '运算符号有误';

    }
    echo $a.$c.$b.'='.$res;
    echo '<br/>';

    //用函数来表示
    //function是关键字,函数的固定格式 不能更改 jisuan是函数名 $a,$b,$c是函数的参数列表(形参)
    function jisuan($a,$b,$c){
     //然后就把我们前面写的代码套用进去就好,函数就是这么简单,加个函数的外壳,里面的代码还是正常写
     if($c == '+'){
        $res = $a+$b;
    }else if($c == '-'){
        $res  = $a-$b;
    }else if($c == '*'){
        $res = $a*$b;
    }else if($c == '/'){
        $res = $a/$b;
    }
    return $a.$c.$b.'='.$res;
    }
    echo jisuan(10,30,'*');

  • 相关阅读:
    SQLite增删改查(自己写SQL语句)
    Why you have so few friends?
    android数据库SQLite简单测试
    C语言 stringcpy,stringcat,stringcmp实现
    python 日期和时间
    Python continue 语句
    Python break 语句
    Python 循环语句
    Python 条件语句
    Python比较运算符
  • 原文地址:https://www.cnblogs.com/ctx1989/p/6079953.html
Copyright © 2020-2023  润新知