批改状态:合格
老师批语:完成的不错

跟双色球的区别就两个地方:
<?phpdefine('RED_BALLS', range(0, 9));define('BLUE_BALLS', range(0, 14));class SevenStarsBall{// 红球private static $redBalls = RED_BALLS;// 蓝球private static $blueBalls = BLUE_BALLS;// 红球中奖号: 1/10, 重复6次private static $electRedBalls = [];// 蓝球中奖号: 1/15private static $electBlueBall;// 保存试机号public static $testNos = [];// 从33个红球中取出6个红球private static function createRedBalls(){// 1. 33选6for ($i = 0; $i < 6; $i++) {// 1. 随机取1个,返回一个key$key = array_rand(self::$redBalls);// 2. 将这个球添加到红球数组中array_push(self::$electRedBalls, self::$redBalls[$key]);// 3. 将选出的红球从原始的33个红球中删除,剩下的再次随机抽取// 重复操作6次,所以不用移除// array_splice(self::$redBalls, $key, 1);}// 2. 排序输出// 也不用排序// sort(self::$electRedBalls, SORT_NUMERIC);}// 从16个蓝球中取出1个蓝球private static function createBlueBall(){self::$electBlueBall = array_rand(array_flip(self::$blueBalls));}// 生成试机号private static function createTestNos($n = 5){for ($i = 0; $i < $n; $i++) {self::$redBalls = RED_BALLS;self::$blueBalls = BLUE_BALLS;self::$electRedBalls = [];// 生成红球self::createRedBalls();// 生成蓝球self::createBlueBall();// 红蓝球合并生成一个中奖试机号self::$testNos[] = array_merge(self::$electRedBalls, [self::$electBlueBall]);}}// 获取试机号,供外部调用(外部只需要访问它,也就是只有它是公开的)public static function getTestNos($n=5){self::createTestNos($n);return self::$testNos;}}
前端代码
<?phprequire __DIR__ . '/sevenstars.php';// 生成数量应该从当前URL中获取// $n = isset($_GET['n']) ? $_GET['n'] : 5;// null合并运算符$n = $_GET['n'] ?? 5;// print_r($n);$allBalls=SevenStarsBall::getTestNos($n);// print_r(DoubleColorBall::$testNos);?><!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>生成七星彩中奖试机号</title><link rel="stylesheet" href="style.css" /></head><body><div class="lottery"><h2>七星彩试机号</h2><form><input type="number" name="n" min="1" value="<?=$_GET['n'] ?? 5?>" onchange="isVip(this)"><button>生成试机号</button></form><table><tbody><?php foreach ($allBalls as $key=> $balls): ?><tr><!-- 第一列是序号: 默认从1开始 $key+1 --><td><?=$key+1?></td><?php foreach ($balls as $ball) : ?><td><?=$ball?></td><?php endforeach; ?></tr><?php endforeach ?></tbody></table><p>福利彩票大家买,众手托起幸福来</p></div><script>function isVip(input) {if (input.value > 10) {alert('免费用户只能看10个,看更多升级到VIP,现在升级8折');input.value = 10;input.max = 10;location.reload(true);return false;}}</script></body></html>

<a href="<?=$_SERVER['PHP_SELF']. '?page='. ( ($page-1) > 1 ? ($page-1) : 1 ) ?>">上一页</a><a href="<?=$_SERVER['PHP_SELF']. '?page='. ( ($page+1) < $pages ? ($page+1) : $pages )?>">下一页</a>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号