PHP无数据库实现学生成绩表 支持最多5名学生添加及平均分统计
前置说明
原有代码存在语法错误、逻辑错误(比如学生类为抽象类无法实例化、班级类错误继承学生类、方法定义在类外部等),已修复后完整代码如下:
1. 修正后的Aluno.php
<?php // 移除abstract修饰支持实例化学生对象 class Aluno { protected $nome, $RA, $nota1, $nota2, $nota3, $nota4; // 所有set方法改为public支持外部调用 public function setNome($nome):bool { if(is_string($nome) && trim($nome) !== ''){ $this->nome = $nome; return true; } return false; } public function setRA($RA):bool { if(is_int($RA) && $RA > 0){ $this->RA = $RA; return true; } return false; } // 优化分数判断逻辑,兼容整数、浮点数类型的分数 public function setNota1($nota1):bool{ if(is_numeric($nota1) && $nota1 >=0 && $nota1 <=10){ $this->nota1 = (float)$nota1; return true; } return false; } public function setNota2($nota2):bool { if(is_numeric($nota2) && $nota2 >=0 && $nota2 <=10){ $this->nota2 = (float)$nota2; return true; } return false; } public function setNota3($nota3):bool { if(is_numeric($nota3) && $nota3 >=0 && $nota3 <=10){ $this->nota3 = (float)$nota3; return true; } return false; } public function setNota4($nota4):bool { if(is_numeric($nota4) && $nota4 >=0 && $nota4 <=10){ $this->nota4 = (float)$nota4; return true; } return false; } // 新增get方法用于读取学生属性 public function getNome() { return $this->nome; } public function getRA() { return $this->RA; } public function getNota1() { return $this->nota1; } public function getNota2() { return $this->nota2; } public function getNota3() { return $this->nota3; } public function getNota4() { return $this->nota4; } // 计算学生个人平均分 public function getMedia():float { return round(($this->nota1 + $this->nota2 + $this->nota3 + $this->nota4)/4, 2); } } ?>
2. 修正后的Turma.php
<?php require_once('Aluno.php'); // 取消错误的继承关系,班级为学生对象的容器 class Turma { // 班级名称属性 protected string $className; // 学生对象存储数组 protected array $alunos = []; // 构造方法初始化班级名称 function __construct(string $className) { $this->className = $className; } // 添加学生方法,最多支持5个学生,返回布尔值 public function addAluno(Aluno $aluno):bool { if(count($this->alunos) < 5){ $this->alunos[] = $aluno; return true; } return false; } // 获取所有学生列表 public function getAlunos():array { return $this->alunos; } // 获取班级名称 public function getClassName():string { return $this->className; } // 计算班级整体平均分 public function getClassMedia():float { $total = 0; $count = count($this->alunos); if($count === 0) return 0; foreach($this->alunos as $aluno){ $total += $aluno->getMedia(); } return round($total / $count, 2); } } ?>
3. index.php 实现代码
<?php require_once('Turma.php'); // 实例化班级 $turma = new Turma("高二3班"); // 预定义5个学生的测试数据,可自行修改 $alunosData = [ ['nome' => '张三', 'RA' => 2024001, 'notas' => [8.5, 7.0, 9.2, 6.8]], ['nome' => '李四', 'RA' => 2024002, 'notas' => [7.5, 8.0, 6.5, 9.0]], ['nome' => '王五', 'RA' => 2024003, 'notas' => [9.0, 8.5, 7.8, 8.2]], ['nome' => '赵六', 'RA' => 2024004, 'notas' => [6.0, 7.5, 8.0, 7.2]], ['nome' => '孙七', 'RA' => 2024005, 'notas' => [8.8, 9.2, 8.5, 9.0]], // 取消下方注释可测试超过5人时的红色错误提示 // ['nome' => '周八', 'RA' => 2024006, 'notas' => [7.0, 6.5, 7.5, 8.0]], ]; // 批量实例化学生并添加到班级 foreach($alunosData as $data){ $aluno = new Aluno(); // 为学生属性赋值 $aluno->setNome($data['nome']); $aluno->setRA($data['RA']); $aluno->setNota1($data['notas'][0]); $aluno->setNota2($data['notas'][1]); $aluno->setNota3($data['notas'][2]); $aluno->setNota4($data['notas'][3]); // 添加到班级并判断结果 $addResult = $turma->addAluno($aluno); if($addResult){ echo "<p>学生【{$data['nome']}】添加成功</p>"; }else{ echo "<p style='color:red;'>错误:学生【{$data['nome']}】添加失败,班级人数已达5人上限</p>"; } } ?> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title><?php echo $turma->getClassName() ?>成绩表</title> <style> table{border-collapse: collapse; width: 80%; margin: 20px auto;} th,td{border:1px solid #ccc; padding: 8px; text-align: center;} th{background-color: #f5f5f5;} h2{text-align: center; margin-top: 30px;} </style> </head> <body> <h2><?php echo $turma->getClassName() ?>学生成绩统计表</h2> <table> <tr> <th>姓名</th> <th>RA学号</th> <th>第一单元成绩</th> <th>第二单元成绩</th> <th>第三单元成绩</th> <th>第四单元成绩</th> <th>个人平均分</th> </tr> <?php foreach($turma->getAlunos() as $aluno): ?> <tr> <td><?php echo $aluno->getNome() ?></td> <td><?php echo $aluno->getRA() ?></td> <td><?php echo $aluno->getNota1() ?></td> <td><?php echo $aluno->getNota2() ?></td> <td><?php echo $aluno->getNota3() ?></td> <td><?php echo $aluno->getNota4() ?></td> <td><?php echo $aluno->getMedia() ?></td> </tr> <?php endforeach; ?> <tr> <td colspan="6" style="text-align: right; font-weight: bold;">班级整体平均分</td> <td style="font-weight: bold;"><?php echo $turma->getClassMedia() ?></td> </tr> </table> </body> </html>
内容的提问来源于stack exchange,提问作者markmark
相关产品推荐
相关产品推荐

