"HR30 Day 17: More Exceptions/PHP"의 두 판 사이의 차이

(새 문서: ==개요== * HR30 Day 17: More Exceptions <source lang='php'> <?php //Enter your code here class Calculator { function power($n,$p) { if( $n<0 || $p<0 ) {...)
 
 
29번째 줄: 29번째 줄:
?>
?>
</source>
</source>
==같이 보기==
* [[HR30 Day 16: Exceptions - String to Integer/PHP]]


[[분류: 30 Days of Code]]
[[분류: 30 Days of Code]]

2018년 8월 14일 (화) 09:49 기준 최신판

1 개요[ | ]

<?php
//Enter your code here
class Calculator {
    function power($n,$p) {
        if( $n<0 || $p<0 ) {
            throw new Exception("n and p should be non-negative");
        }
        return pow($n, $p);
    }
}
$myCalculator=new Calculator();
$T=intval(fgets(STDIN));
while($T-->0){
    list($n, $p)  = explode(" ", trim(fgets(STDIN)));
    try{
        $ans=$myCalculator->power($n,$p);
        echo $ans."\n";
    }
    catch(Exception $e){
        echo $e->getMessage()."\n";
    }
}
?>

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}