Home >Backend Development >PHP Tutorial >php 的 FizzBuzzWhizz

php 的 FizzBuzzWhizz

WBOY
WBOYOriginal
2016-07-25 08:48:071177browse
来源:其他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();
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn