简体   繁体   English

模具滚压程序php

[英]Die Rolling Program php

I am in a class at IUPUI and I was given these instructions 我在IUPUI上课,得到了这些指示

  1. In NetBeans, create a new PHP Web Page named index.php in the htdocs\\I210\\Lab04 folder. 在NetBeans中,在htdocs \\ I210 \\ Lab04文件夹中创建一个名为index.php的新PHP网页。
  2. Change the document title to “Statistical analysis of results from rolling a six‐sided die”; 将文档标题更改为“对六面模具滚动结果的统计分析”;
  3. At the very top of the page, add the following PHP code block and comments. 在页面的顶部,添加以下PHP代码块和注释。

     <?php /* * Author: Your name * Date: Today's date */ ?> 
  4. 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标题,该标题在页面主体的开头显示为“对滚动六面模具的结果进行统计分析”。

  5. Inside the PHP code block, create six variables to store the frequency of each side of the die. 在PHP代码块内,创建六个变量以存储管芯每一侧的频率。 Choose your variable names wisely. 明智地选择变量名。 For example, you may name them $frequency1 , $frequency2 …… 例如,您可以将它们命名为$frequency1$frequency2 ……
  6. 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);

  7. 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。

  8. Use a loop structure (FOR, WHILE, or DO … WHILE) to repeatedly execute the PHP statements in the last two steps 5000 times. 使用循环结构(FOR,WHILE或DO…WHILE)在最后两个步骤中重复执行PHP语句5000次。
  9. Use a table to display die faces and their frequencies occurred during the 5000 times of rolls. 使用表格显示模具表面及其频率在5000次滚动中发生。
  10. Below the table, add a refresh button. 在表格下方,添加刷新按钮。

     <input type="submit" value="Refresh" onclick="window.location.reload()" /> 
  11. Add CSS to center everything on the page. 添加CSS将所有内容置于页面中心。

  12. Thoroughly test your page. 彻底测试您的页面。 Clicking the “refresh” button should generate a new set of frequencies. 单击“刷新”按钮应生成一组新的频率。 Note: your frequencies could be different from mine." 注意:您的频率可能与我的频率不同。”

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM