0

我正在尝试为一组变量制作评分量表。我遇到了一些问题,目前完全脑死亡。任何人都可以就如何实现这一点给我一些帮助吗?非常感谢!

function ovr_grade($talent, $physical, $entertainment, $reputation, $overness) {
if ($talent || $physical || $entertainment || $reputation || $overness >= 90) {
return "Grade: A";
} elseif ($talent || $physical || $entertainment || $reputation || $overness >= 80) {
return "Grade: B";
} elseif ($talent || $physical || $entertainment || $reputation || $overness >= 70) {
return "Grade: C";
} elseif ($talent || $physical || $entertainment || $reputation || $overness >= 60) {
return "Grade: D";
} elseif ($talent || $physical || $entertainment || $reputation || $overness >= 50) {
return "Grade: E";
} elseif ($talent || $physical || $entertainment || $reputation || $overness <= 49) {
return "Grade: F";
} else {
return "N/A";
}
}

echo ovr_grade();
4

8 回答 8

2

尝试:

if (max($talent,$physical,$entertainment,$reputation) >= 90 )
{
       return "Grade: A";
}
elseif ...........
于 2013-07-18T03:57:01.340 回答
1
function ovr_grade($talent, $physical, $entertainment, $reputation, $overness) {
    $values = array($talent, $physical, $entertainment, $reputation, $overness);
    $average = array_sum($values) / count($values);
    if ($average >=90)
        return "Grade: A";
    if ($average >=80)
        return "Grade: B";
    if ($average >=70)
        return "Grade: C";
    if ($average >=60)
        return "Grade: D";
    if ($average >=50)
        return "Grade: E";
    if ($average <=49)
        return "Grade: F";
    else
        return "N/A";
}
于 2013-07-18T03:55:25.247 回答
0

我认为您应该将条件调整为:

function ovr_grade($talent, $physical, $entertainment, $reputation, $overness) {
if ($talent >= 90 || $physical >= 90 || $entertainment >= 90 || $reputation >= 90 || $overness >= 90) {
return "Grade: A";
} elseif ($talent >= 80 || $physical >= 80 || $entertainment >= 80 || $reputation >= 80 || $overness >= 80) {
return "Grade: B";
} elseif ($talent >= 70 || $physical >= 70 || $entertainment >= 70 || $reputation >= 70 || $overness >= 70) {
return "Grade: C";
} elseif ($talent >= 60 || $physical >= 60 || $entertainment >= 60 || $reputation >= 60 || $overness >= 60) {
return "Grade: D";
} elseif ($talent >= 50 || $physical >= 50 || $entertainment >= 50 || $reputation >= 50 || $overness >= 50) {
return "Grade: E";
} elseif ($talent <= 49 || $physical <= 49 || $entertainment <= 49 || $reputation <= 49 || $overness <= 49) {
return "Grade: F";
} else {
return "N/A";
}
}

echo ovr_grade(90,90,90,90,90);//input their respective values

因此,每个值都将与数字值进行比较,而不是布尔值。

于 2013-07-18T03:53:46.100 回答
0

首先,您写了一些关于您希望从此功能获得的结果的要求的详细信息。我检查了你的功能,它有一些主要的条件问题。

现在,如果您的要求是在某人在任何类别中获得 90 分时显示“等级:A”(例如,$talent 中的 90 或 $physical 中的 90 或 $entertainment 中的 90 等),那么您编写的功能可以正常工作。您需要做的就是初始化函数,例如:

function ovr_grade($talent, $physical, $entertainment, $reputation, $overness) {
if ($talent >= 90 || $physical >= 90 || $entertainment >= 90 || $reputation >= 90 || $overness >= 90) {
return "Grade: A";
} elseif ($talent >= 80 || $physical >= 80 || $entertainment >= 80 || $reputation >= 80 || $overness >= 80) {
return "Grade: B";
} elseif ($talent >= 70 || $physical >= 70 || $entertainment >= 70 || $reputation >= 70 || $overness >= 70) {
return "Grade: C";
} elseif ($talent >= 60 || $physical >= 60 || $entertainment >= 60 || $reputation >= 60 || $overness >= 60) {
return "Grade: D";
} elseif ($talent = 50 || $physical = 50 || $entertainment = 50 || $reputation = 50 || $overness >= 50) {
return "Grade: E";
} elseif ($talent <= 49 || $physical <= 49 || $entertainment <= 49 || $reputation <= 49 || $overness <= 49) {
return "Grade: F";
} else {
return "N/A";
}
}

echo ovr_grade(90,90,90,90,90);

然后你应该得到想要的结果。但是,如果您想在有人在所有类别中获得 90 分时显示“等级:A”,那么您应该编辑您的条件,如下所示:

function ovr_grade($talent, $physical, $entertainment, $reputation, $overness) {
if ($talent >= 90 && $physical >= 90 && $entertainment >= 90 && $reputation >= 90 && $overness >= 90) {
return "Grade: A";
} elseif ($talent >= 80 && $physical >= 80 && $entertainment >= 80 && $reputation >= 80 && $overness >= 80) {
return "Grade: B";
} elseif ($talent >= 70 && $physical >= 70 && $entertainment >= 70 && $reputation >= 70 && $overness >= 70) {
return "Grade: C";
} elseif ($talent >= 60 && $physical >= 60 && $entertainment >= 60 && $reputation >= 60 && $overness >= 60) {
return "Grade: D";
} elseif ($talent = 50 && $physical = 50 && $entertainment = 50 && $reputation = 50 && $overness >= 50) {
return "Grade: E";
} elseif ($talent <= 49 && $physical <= 49 && $entertainment <= 49 && $reputation <= 49 && $overness <= 49) {
return "Grade: F";
} else {
return "N/A";
}
}

echo ovr_grade(90,90,90,90,90);

我想这会帮助你。如果需要更多,请不要犹豫。

谢谢

于 2013-07-18T04:04:23.427 回答
0

我 aaprreciate “James Anderson” 回答所以 +1 他,但 max() 计算可以减少

$grades=  max($talent,$physical,$entertainment,$reputation);

if($grades>90)
{
return "Grade: A";
} 
elseif ($grades>80)
{
return "Grade: B";
} 
.
.
.
.
.
.
else 
{
return "N/A";
}

更新:

不幸的是,无论如何我都无法深入了解您的“评论”,我认为您需要一个通用函数来返回每个因素的等级(例如:物理,娱乐......)

function  getGrades($factor)
{

$grades= $factor;

if($grades>90)
{
return "Grade: A";
} 
elseif ($grades>80)
{
return "Grade: B";
} 
.
.
.
.
.
.
else 
{
return "N/A";
}
}

并打电话给每个人打分

echo "Talent : " . getGrades($talent);
echo "Physical : " . getGrades($physical);
于 2013-07-18T04:10:58.077 回答
0

您可以将其修改为:

function ovr_grade($talent, $physical, $entertainment, $reputation, $overness) {
    $max_val = max($talent, $physical, $entertainment, $reputation, $overness);

    if ($max_val >= 90) {
        return "Grade A";
    } elseif ($max_val >= 80) {
        return "Grade B";
    } elseif ($max_val >= 70) {
        return "Grade C";
    } elseif ($max_val >= 60) {
        return "Grade D";
    } elseif ($max_val >= 50) {
        return "Grade E";
    } elseif ($max_val < 50) {
        return "Grade F";
    } else {
        return "N/A";
    }
于 2013-07-18T04:14:48.453 回答
0

我认为没有必要返回“N/A”;当没有什么大于 50 时。只返回“Grade:F”

于 2013-07-18T04:28:27.967 回答
0

受到詹姆斯的启发

 $grade = array(
                        'A'=>90,
                        'B'=>80,
                        'C'=>70,
                        'D'=>60,
                        'E'=>50,
                        'F'=>40,                
                        );

        $obtained = max($talent,$physical,$entertainment,$reputation);
        $grading_flag = floor($obtained / 10)*10;
        echo $grade[$gr];

    /* ambiguous about your fail  status */
于 2013-07-18T04:40:52.610 回答