当前位置:Gxlcms > PHP教程 > php的FizzBuzzWhizz

php的FizzBuzzWhizz

时间:2021-07-01 10:21:17 帮助过:6人阅读

来源:其他FizzBuzzWhizz :http://www.oschina.net/code/snippet_1414453_35450;

正则复习 php class,写的不太好,抛砖引玉 。
  1. class NumberGame{
  2. private $str="start
    ";
  3. private $num=0;
  4. private $Fnum=0;
  5. private $snumarr=array();
  6. private $arr_key=array();
  7. function __construct($Fnum,$num,$snumarr){
  8. if($Fnum>=1 && $num>$Fnum && is_array($snumarr) && count($snumarr)>0){
  9. //$this->Fnum=$Fnum;
  10. //$this->num=$num;
  11. $this->snumarr=$snumarr;
  12. for($i=$Fnum;$i<=$num;$i++){
  13. //rule 5
  14. $this->arr_key=array_keys($snumarr);
  15. $typeB = strpbrk($i, current($this->arr_key) )?true:false;
  16. if($typeB){
  17. $this->str.= current($snumarr)." (".$i.")
    ";
  18. }else{
  19. $this->rule($i);
  20. }
  21. }
  22. }
  23. }
  24. function rule($i){
  25. $str="";
  26. for($j=0;$jsnumarr);$j++){
  27. $key=$this->arr_key[$j];
  28. //var_dump($key);
  29. if(is_int($i/$key)){
  30. $str.=$this->snumarr[$key];
  31. }
  32. }
  33. if(empty($str)){
  34. $str=$i."
    ";
  35. }else{
  36. $str.= " (".$i.")
    ";
  37. }
  38. $this->str.=$str;
  39. }
  40. function answer(){
  41. return $this->str;
  42. }
  43. }
  44. $snumarr=array('3'=>'Fizz','5'=>'Buzz','7'=>'Whizz');
  45. $ng=new NumberGame(1,100,$snumarr);
  46. echo $ng->answer();

人气教程排行