自动化出算术题-拿给儿子练速算吧 o(∩_∩)o

php中文网
发布: 2016-07-25 08:49:51
原创
1073人浏览过
自定义范围,自定义运算符,自定义运算次数。太牛逼了
  1. /**
  2. * Description of QuestionEngine
  3. * 出题引擎一枚
  4. *
  5. * @author lyc
  6. * @copyright (c) 2013, Unary Inc.
  7. */
  8. class QuestionEngine {
  9. /**
  10. * 出题范围
  11. * @var string $scope
  12. */
  13. public $scope = array(1, 100);
  14. /**
  15. * 包含的运算符,含有多个则混合出题
  16. * @var string $operators
  17. */
  18. public $operators = '+-';
  19. /**
  20. * 运算次数
  21. * @var int
  22. */
  23. public $optTimes = 1;
  24. public function generate() {
  25. //按运算次数产生一组数值
  26. start:
  27. for ($index = 0; $index optTimes + 1; $index++) {
  28. $elements[] = $this->randomValue();
  29. }
  30. $operatorType = strlen($this->operators); //有几个运算符供选择
  31. //开始组装算式
  32. $question = '';
  33. for ($index = 0; $index $question.=' ' . $elements[$index] . ' '; //放一个数字进来
  34. if ($index $question.=substr($this->operators, mt_rand(0, $operatorType - 1), 1);
  35. }
  36. eval('$anwser = ' . $question . ';');
  37. if ($anwser $elements = array();
  38. goto start; //需PHP5.3的支持
  39. }
  40. echo "$question= " . $anwser;
  41. }
  42. /**
  43. * 产生一个范围内的随机值
  44. *
  45. * @return int
  46. */
  47. protected function randomValue() {
  48. return mt_rand($this->scope[0], $this->scope[1]);
  49. }
  50. }
复制代码
  1. include 'QuestionEngine.class.php';
  2. $hello = new QuestionEngine();
  3. $hello->generate();
  4. ?>
  5. 结果:26 + 85 = 111
复制代码


最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号