【问题标题】:php array 6 sided Dice Roll Tally issue [closed]php数组6面骰子掷骰子问题[关闭]
【发布时间】:2015-06-29 01:30:34
【问题描述】:

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

<?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

【问题讨论】:

  • 欢迎来到 Stack Overflow!这个问题的信息有点短。你遇到了什么问题?
  • 你得到了什么结果?
  • $tally 是一个数组。 echo $tally 只会吐出 6000 个单词 Array

标签: php arrays dice


【解决方案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在骰子结果的每个循环中返回一次。相反,等到循环结束然后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;

在这里你开始构建,然后切换到echoing,同样,你不会从这个函数返回任何东西。因此,不要echo 获取结果,而是在$line 变量中继续捕获它们,然后在函数末尾回显它。

【讨论】:

  • #2 将导致E_NOTICE : type 8 -- Undefined offset。可以通过$tally[$number] =(isset($tally[$number])?$tally[$number]++:1; 阻止
  • @Sean 感谢您对通知的提醒,但是您提供的内容不起作用。我想你的意思是:$tally[$number] = (isset($tally[$number]))? $tally[$number] + 1: 1
  • @bloddyKnuckles - 你是对的。
  • 谢谢@bloodyKnuckles,你帮了很多忙!它正在工作!
猜你喜欢
  • 2018-09-08
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 2013-06-12
  • 2012-09-16
  • 1970-01-01
  • 2012-11-27
  • 2021-10-07
相关资源
最近更新 更多