You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP成绩计算器无法输出各分类得分百分比的技术问询

PHP成绩计算器:修复得分百分比输出问题

你的代码没法输出各项目得分百分比,核心问题是输出语句里的占位符X没替换成实际计算的百分比值,而且你也没提前计算并存储这些百分比。

修改步骤及完整代码

  1. 先计算每个项目的得分百分比并保存到变量
  2. 把输出语句里的X替换成对应的百分比变量
  3. 额外添加除以0的判断(避免满分输入0导致报错),同时用number_format格式化百分比,保留两位小数更直观
<?php
$par_Earned = $_POST['earnedParticipation'];
$par_Possible = $_POST['maxParticipation'];
$par_Weight = $_POST['weightParticipation'];
$q_Earned = $_POST['earnedQuiz'];
$q_Possible = $_POST['maxQuiz'];
$q_Weight = $_POST['weightQuiz'];
$l_Earned = $_POST['earnedLab'];
$l_Possible = $_POST['maxLab'];
$l_Weight = $_POST['weightLab'];
$p_Earned = $_POST['earnedPracticum'];
$p_Possible = $_POST['maxPracticum'];
$p_Weight = $_POST['weightPracticum'];

function percentage($a, $b) {
    // 处理满分为0的情况,避免除以0报错
    if ($b == 0) {
        return 0;
    }
    $percent = ($a / $b) * 100;
    return $percent;
}

function weightValue($c, $d, $e) {
    $weight = (percentage($c, $d) * $e) / 100;
    return $weight;
}

// 计算各项目的得分百分比
$parPercent = percentage($par_Earned, $par_Possible);
$qPercent = percentage($q_Earned, $q_Possible);
$lPercent = percentage($l_Earned, $l_Possible);
$pPercent = percentage($p_Earned, $p_Possible);

// 计算各项目加权值
$parWeight = weightValue($par_Earned, $par_Possible, $par_Weight);
$quizWeight = weightValue($q_Earned, $q_Possible, $q_Weight);
$labWeight = weightValue($l_Earned, $l_Possible, $l_Weight);
$pracWeight = weightValue($p_Earned, $p_Possible, $p_Weight);
$total = $parWeight + $quizWeight + $labWeight + $pracWeight;

function lettergrade($total) {
    if ($total >= 95) {
        $lettergrade = "A+";
    } else if ($total < 95 && $total >= 90) {
        $lettergrade = "A";
    } else if ($total < 90 && $total >= 85) {
        $lettergrade = "B+";
    } else if ($total < 85 && $total >= 80) {
        $lettergrade = "B";
    } else if ($total < 80 && $total >= 75) {
        $lettergrade = "C+";
    } else if ($total < 75 && $total >= 70) {
        $lettergrade = "C";
    } else if ($total < 70 && $total >= 60) {
        $lettergrade = "D";
    } else if ($total < 60 && $total >= 0) {
        $lettergrade = "F";
    } else {
        $lettergrade = "成绩无效";
    }
    return $lettergrade;
}

// 替换占位符,格式化输出内容
echo "<p>参与项得分率:" . number_format($parPercent, 2) . "%,权重占比:" . $par_Weight . "% </p>";
echo "<p>测验项得分率:" . number_format($qPercent, 2) . "%,权重占比:" . $q_Weight . "% </p>";
echo "<p>实验项得分率:" . number_format($lPercent, 2) . "%,权重占比:" . $l_Weight . "% </p>";
echo "<p>实习项得分率:" . number_format($pPercent, 2) . "%,权重占比:" . $p_Weight . "% </p>";
echo "<p><b>你的最终成绩:" . number_format($total, 2) . "%,对应等级:" . lettergrade($total) . "</b></p>";
?>

关键修改点

  • 新增$parPercent、$qPercent等变量,调用percentage函数计算各项目得分率
  • 替换输出语句中的占位符X,用number_format规范百分比显示格式
  • 在percentage函数中增加满分0的判断,避免运行时报错
  • 调整输出文本的中文表述,更符合使用习惯

内容的提问来源于stack exchange,提问作者o9ka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 07:35:18