zoukankan      html  css  js  c++  java
  • PHP数组练习(2)

    题目:在输入框内输入5个数(输入那5个数用空格隔开),请用一维数组来编写一个程序求出他们的平均值(保留2位小数位)。大家可以自己想想怎么做。

    参考代码:

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
    </head>
    <body>
    <h3>请输入5个数,并且用空格隔开</h3>
    
    <?php
    
        //接收提交(输入过来的值) REQUEST
    
        $grade =@$_REQUEST['grade'];
        //echo $grades;
    
        //拆分成数组
        $grades = explode(" ",$grade);
    
        //print_r($grades);
        //定义一个变量存放总数
        $total=0;
        //计算总分
        for($i=0;$i<count($grades);$i++){
        
            $total +=$grades[$i];    
        }
        
        //round函数 保留小数位||这是四舍五入法保留2位小数
        //echo '平均数为:'.round($total/count($grades),2);
    ?>
    
    <form action="array01.php" method="post">
        请输入:<input type="text" name="grade" value="<?php echo $grade;?>">
        <input type="submit" value="请提交">
    </form>
    <?php
        //number_format()函数 是保留位数,同时格式化数字加千位符
        echo '平均数为:'.number_format($total/count($grades),2);
        //echo '平均数为:'.round($total/count($grades),2);
    ?>
    </body>
    </html>
  • 相关阅读:
    ubuntu下erlang man的安装
    ranch分析学习(四)
    ranch分析学习(三)
    ranch分析学习(二)
    ranch分析学习(一)
    IIS 配置错误解决方法集合
    Visual Studio 2013中添加mimeType
    wordpress 开发日志及技巧收集
    css3 动画
    高宽比例计算方法及等比高宽修改计算方法
  • 原文地址:https://www.cnblogs.com/pwm5712/p/2968797.html
Copyright © 2011-2022 走看看