[英]Die Rolling Program php
I am in a class at IUPUI and I was given these instructions 我在IUPUI上课,得到了这些指示
At the very top of the page, add the following PHP code block and comments. 在页面的顶部,添加以下PHP代码块和注释。
<?php /* * Author: Your name * Date: Today's date */ ?>
Inside the body section, create a H2 heading that reads “Statistical analysis of results from rolling a six‐sided die” at the beginning of the page body. 在主体部分的内部,创建一个H2标题,该标题在页面主体的开头显示为“对滚动六面模具的结果进行统计分析”。
$frequency1
, $frequency2
…… $frequency1
, $frequency2
…… To simulate rolling a die, use the built‐in PHP function named rand($min, $max)
to generate a random number between 1 and 6, 1 being Face 1, 2 being Face 2, and so on. 要模拟模具的滚动,请使用名为
rand($min, $max)
的内置PHP函数生成一个1到6之间的随机数,其中1为Face 1,2为Face 2,依此类推。 The following statement generates a random number between 1 and 6 and stores it in the variable called $face
. 以下语句生成一个1到6之间的随机数,并将其存储在名为
$face
的变量中。 $face = rand(1, 6);
Use a conditional structure (IF or SWITCH) to increment frequency for each side of the die occurred. 使用条件结构(IF或SWITCH)来增加发生在芯片每一侧的频率。 For example, if the value of
$face
is 1, increment $frequence1 by 1. 例如,如果
$face
值为1,则将$ frequence1递增1。
Below the table, add a refresh button. 在表格下方,添加刷新按钮。
<input type="submit" value="Refresh" onclick="window.location.reload()" />
Add CSS to center everything on the page. 添加CSS将所有内容置于页面中心。
I'm not asking for someone to do the work for me because I have most of the work done. 我不是要别人为我做这项工作,因为我已经完成了大部分工作。 I can't seem to figure out how to make my code loop 5000 times.
我似乎无法弄清楚如何使我的代码循环5000次。 This is my code currently.
这是我目前的代码。
<!DOCTYPE html> <html> <head> <title>Statistical analysis of results from rolling a six‐sided die</title> </head> <body> <h2>Statistical analysis of results from rolling a six‐sided die</h2> <?php $frequency1=0; $frequency2=0; $frequency3=0; $frequency4=0; $frequency5=0; $frequency6=0; $face = rand(1, 6); if ($face==1) { ++$frequency1; } else if ($face==2) { ++$frequency2; } else if ($face==3) { ++$frequency3; }else if ($face==4) { ++$frequency4; }else if ($face==5) { ++$frequency5; }else if ($face==6) { ++$frequency6; } echo "<table> <tr> <th>Face</th> <th>Frequency</th> </tr>"; $face_num=6; $face_count=1; while ($face_count<=$face_num) { $frequency = ${'frequency' . $face_count}; echo "<tr> <td> $face_count </td> <td> $frequency </td> </tr>"; $face_count++; } ?> </table> <input type="submit" value="Refresh" onclick="window.location.reload()" /> </body> </html>
You can use a for()
loop to execute the code 5000 times. 您可以使用
for()
循环执行该代码5000次。
$frequency = [0, 0, 0, 0, 0, 0];
for($i = 1; $i <= 5000; $i++):
$face = rand(0, 5); // index's start at 0
++$frequency[$face];
endfor;
for($i = 0; $i <= count($frequency) -1; $i++):
$n = $i +1;
echo "Frequency {$n} is equal to {$frequency[$i]}";
endfor;
for readability, I added the use of an array so its easier to manage in the future. 为了提高可读性,我添加了数组的使用,以便将来更易于管理。
Running this code 10 times (not 5000) gives a result like this: 运行此代码10次(而不是5000次),结果如下:
Frequency 1 is equal to 1
Frequency 2 is equal to 3
Frequency 3 is equal to 1
Frequency 4 is equal to 3
Frequency 5 is equal to 1
Frequency 6 is equal to 1
Note: You will need to implement this into your own view. 注意:您将需要在自己的视图中实现它。
Here is an example of a view: 这是一个视图示例:
$frequency = [0, 0, 0, 0, 0, 0];
echo "<table>";
for($i = 1; $i <= 10; $i++):
$face = rand(0, 5);
++$frequency[$face];
echo "<tr>";
echo "<th>";
echo "Face";
echo "</th>";
echo "<th>";
echo "Frequency";
echo "</th>";
echo "</tr>";
echo "<tr>";
echo "<th>";
echo "{$face}";
echo "</th>";
echo "<th>";
echo "{$frequency[$face]}";
echo "</th>";
echo "</tr>";
endfor;
echo "</table>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.