Webbserverprogrammering 1

Källkod

Följande filer och mappar finns under mappen webbserverprogrammering.
Mappar visas till vänster och filer till höger. Klicka på en fil eller mapp för att öppna nedan eller visa dess innehåll.

webbserverprogrammering/exercises/classes_dice/

10 filer

Dice.php
DiceImage.php
Histogram.php
dice_1.php
dice_2.php
dice_3.php
dice_4.php
dice_5.php
dice_6.php
dice_7.php

dice_2.php

71 lines UTF-8 Windows (CRLF)
<?php
error_reporting
(-1); // Report all type of errors
ini_set('display_errors'1); // Display all errors 
ini_set('output_buffering'0); // Do not buffer outputs, write directly
?>

<!DOCTYPE html>
<html lang="sv">
<head>
 <title>Tärning 2</title>
 <meta charset="utf-8">
 <style type="text/css">
  body {
   font-family: Arial;
  }
 </style>
</head>
<body>
 <h2>En tärning</h2>
 <p>Tärningen med 6 sidor kastas 6 gånger. Denna gång m.h.a. en "Dice"-klass!</p>
 
 <?php


 
class Dice {
  private 
$sides;
  private 
$rolls = [];

  function 
__construct($sides 6) {
   
$this->sides $sides;
  }

  function 
roll($times 1) {
   for (
$i 0$i $times$i++) { 
    
$this->rolls[count($this->rolls)] = rand(1$this->sides);
   }
  }

  function 
printRolls() {
   echo 
"<ul>";
   foreach (
$this->rolls as $roll) {
    echo 
"<li>$roll</li>";
   }
   echo 
"</ul>";
  }

  function 
getSumOfRolls() {
   return 
array_sum($this->rolls);
  }

  function 
getAverageRoll() {
   return 
round($this->getSumOfRolls() / count($this->rolls), 2);
  }
 }


 
$myDice = new Dice(); // default is 6 sides

 
$myDice->roll(6); // roll 6 times

 
$myDice->printRolls();



 
?>

 <p><strong>Summa:</strong> <?= $myDice->getSumOfRolls() ?></p>
 <p><strong>Medelvärde:</strong> <?= $myDice->getAverageRoll() ?></p>

</body>
</html>