php阵列6面骰子卷Tally问题

时间:2015-04-21 17:59:54

标签: php arrays dice

我希望有人能弄清楚为什么我的代码不适用于6面骰子,使用数组计算6000卷的数字。

<?php
//  main    //////////////////////////////////
//$throw_dice = array();
//$tally = array();
echo "debug - code runs to this point<br>\n";

$throw_dice = throw_dice($throw_dice);
$tally = tally_results($throw_dice);
    echo "debug with tally: $tally[4]<br>\n";
    exit;
$line = print_table($tally);
echo $line;


// functions ////////////////////////////////

function throw_dice ($f_throw_dice) {
    /*  required pseudocode:
        for loop from 1 to 6000
            populate each element with random number 1 - 6
        end for loop
        return array
    */
for ($i = 0; $i < 6000; $i++) {
    $f_throw_dice[] = mt_rand(1,6);
}
return array($f_throw_dice);
}

function tally_results ($f_throw_dice) {
    /*  
        use increment system example shown below with associative array:
            $numbers = array(1, 2, 2, 1, 2, 1, 1, 1, 2)
            foreach ($numbers as $number) {$tally[$number]++}
            will give 5 for $tally[1] and 4 for $tally[2]
    */
//$tally = array('1' => 1,'2' => 2,'3' => 3,'4' => 4,'5' => 5,'6' => 6,);
//$numbers = array($f_throw_dice);
//$tally[0] = '1';
//$tally[1] = '2';
//$tally[2] = '3';
//$tally[3] = '4';
//$tally[4] = '5';
//$tally[5] = '6';
$tally = array();
foreach ($f_throw_dice as $number) 
    {
        $tally[$number]++;
        echo $tally;

    }
}


function print_table($f_tally) {
    /*  required pseudocode:
        note: concatenate entire formatted printout in one variable $line
        Can start this way:
            $line = "<pre>";
            $line .= sprintf ("DIE #%10s", 'OCCURS');
            $line .= "\n===============\n";
        sort $f_tally by key
        foreach loop 
            concatenate $line with another $f_tally element using the 
                sprintf format from last assignment
        end loop
        return $line
    */
    $line = "<pre>";
    $line .= sprintf ("DIE #%10s", 'OCCURS');
    $line .= "\n===============\n";
    ksort ($f_tally);
    echo '<ul>';
    foreach ($f_tally as $numbers => $tally) {
        echo '<pre>',
        $line .= sprintf('%s        %s', $numbers, $tally), '</pre>';
       // echo '<li>$numbers ($tally)</li>';
    }
    echo '</ul>';
}
?>

结果应该类似于:

DIE #   OCCURS
==============
1         1000
2          990
3         1038
4         1012
5         1007
6          953

1 个答案:

答案 0 :(得分:1)

一些问题。

1)throw_dice函数中,更改:

return array($f_throw_dice);

...为:

return $f_throw_dice;

$f_throw_dice已经是一个数组。因此,通过将其包装在数组函数中,您将向数组添加一个额外的层,这会导致tally_results函数中的循环失败。因此,只需return $f_throw_dice

2)tally_results函数中,更改:

foreach ($f_throw_dice as $number) 
    {
        $tally[$number]++;
        echo $tally;

    }

...为:

foreach ($f_throw_dice as $number) 
{
    $tally[$number]++;
}
return $tally;

这里你没有从函数返回$tally,而是在骰子结果的每个循环中echo一次。{1}}相反,等待直到循环结束然后return它。

3)然后,在print_table函数中,更改:

echo '<ul>';
foreach ($f_tally as $numbers => $tally) {
    echo '<pre>',
    $line .= sprintf('%s        %s', $numbers, $tally), '</pre>';
   // echo '<li>$numbers ($tally)</li>';
}
echo '</ul>';

...为:

$line .=  '<ul>';
foreach ($f_tally as $numbers => $tally) {
    $line .=  '<pre>';
    $line .= sprintf('%s        %s', $numbers, $tally) .  '</pre>';
   // echo '<li>$numbers ($tally)</li>';
}
$line .=  '</ul>';
return $line;

在这里你开始构建但是然后切换到echo,再次,你不会从这个函数返回任何东西。因此,不是echo结果,而是继续在$line变量中捕获它们,然后在函数结束时回显它。