Created
March 28, 2017 11:10
-
-
Save gustawdaniel/96026a476abcb75f5d71609aeca7c8ac to your computer and use it in GitHub Desktop.
zadanie2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function divide($divider, $dividend) { | |
if ($divider == 0){ | |
throw new InvalidArgumentException("Divide by zero error"); | |
} | |
if ($divider < 0){ | |
throw new OutOfRangeException ("Divide is lover then zero"); | |
} | |
return $dividend / $divider; | |
} | |
function randomDivide($tryNumber){ | |
$invalidCounter = 0; | |
$outCounter = 0; | |
echo "---------------\n"; | |
for ($n = 0; $n < $tryNumber; $n++){ | |
try { | |
echo divide(rand(-10, 10), 5) . "\n"; | |
} catch (InvalidArgumentException $e) { | |
$invalidCounter++; | |
} catch (OutOfRangeException $e) { | |
$outCounter++; | |
} | |
} | |
echo "---------------\n"; | |
echo "| out: " . $outCounter . "\n"; | |
echo "| inv: " . $invalidCounter . "\n"; | |
echo "---------------\n"; | |
} | |
randomDivide(5); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment