要在PHP循环中计算的范围值

时间:2016-06-27 02:24:36

标签: php

我需要计算avg是否> 7.75表示重其他编号

关注点是什么。

需要计算从$ x到$ y范围内的所有数字都来自int。

但我能够得到一个平均值,但不知道如何处理所有数字平均值和存储在数组范围内。

    function heavyDecC ($x,$y)
{

    for($i=$x;$i<=$y;$i++)
    {
        $numb = $i;
        $numleng = strlen((string)$i);
        $int = 0;
        for($j=0;$j<$numleng;$j++)
        {

            $int = $int + ($numb % 10);
            $numb = (int)$numb / 10;
        }
        $avg = 0.00;
        $avg = round($int/$numleng,2);
        if($avg>7)
        {
            $isHeavy = "Yes";
        }
        else
        {
            $isHeavy = "No";
        }

        return $result[] = array($i,$avg,$isHeavy);
    }
}

3 个答案:

答案 0 :(得分:1)

这是一个简单的解决方案

<?php

$results = heavyDecC(2685, 5875);

// Display results like this
foreach ($results as $id){
        echo "--------------------------<br/>";
        foreach($id as $key => $val){
            echo $key . " - " . $val . "<br />";
        }
    }

function heavyDecC ($x,$y) {

    for($i=$x; $i<=$y; $i++){
        $num = $x;
        $isHeavy = "No";
        $num_length = strlen((string)$num);
        $array  = array_map('intval', str_split($num));
        $sum = array_sum($array);
        $average = ($sum / $num_length);

            if($average > 7){
                $isHeavy = "Yes";
            }else {
                $isHeavy = "No";
            }

        $newdata =  array (
              'Number' => $num,
              'average' => $average,
              'is_heavy' => $isHeavy
            );

        $md_array[$i]= $newdata;
        $x++;

    }

return $md_array;
}

?>

这样的结果

--------------------------
Number - 2991
average - 5.25
is_heavy - No
--------------------------
Number - 2992
average - 5.5
is_heavy - No
--------------------------
Number - 2993
average - 5.75
is_heavy - No
--------------------------
Number - 2994
average - 6
is_heavy - No
--------------------------
Number - 2995
average - 6.25
is_heavy - No
--------------------------
Number - 2996
average - 6.5
is_heavy - No
--------------------------
Number - 2997
average - 6.75
is_heavy - No
--------------------------
Number - 2998
average - 7
is_heavy - No
--------------------------
Number - 2999
average - 7.25
is_heavy - Yes
--------------------------

答案 1 :(得分:0)

return移到for循环之外,如下所示:

 function heavyDecC ($x,$y)
{
    $result = array();
    for($i=$x;$i<=$y;$i++)
    {
        $numb = $i;
        $numleng = strlen((string)$i);
        $int = 0;
        for($j=0;$j<$numleng;$j++)
        {

            $int = $int + ($numb % 10);
            $numb = (int)$numb / 10;
        }
        $avg = 0.00;
        $avg = round($int/$numleng,2);
        if($avg>7)
        {
            $isHeavy = "Yes";
        }
        else
        {
            $isHeavy = "No";
        }

        $result[] = array($i,$avg,$isHeavy);
    }
    return $result;
}

答案 2 :(得分:0)

每个循环都有返回值,因此只能获取最后的索引值!所以你需要在循环之外返回....

   function heavyDecC ($x,$y)
{
    $result = array();
    for($i=$x;$i<=$y;$i++)
    {
        $numb = $i;
        $numleng = strlen((string)$i);
        $int = 0;
        for($j=0;$j<$numleng;$j++)
        {

            $int = $int + ($numb % 10);
            $numb = (int)$numb / 10;
        }
        $avg = 0.00;
        $avg = round($int/$numleng,2);
        if($avg>7)
        {
            $isHeavy = "Yes";
        }
        else
        {
            $isHeavy = "No";
        }

        $result[] = array($i,$avg,$isHeavy);
    }
    return $result; // return here outside of loop
}