函数中的PHP变量

时间:2013-07-18 03:46:48

标签: php function variables

我正在尝试为一组变量制作评分等级。我正在遇到一些问题,此刻我完全已经死了。任何人都可以给我一些如何实现这一目标的帮助吗?非常感谢!

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();

8 个答案:

答案 0 :(得分:2)

尝试:

if (max($talent,$physical,$entertainment,$reputation) >= 90 )
{
       return "Grade: A";
}
elseif ...........

答案 1 :(得分: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";
}

答案 2 :(得分: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

所以每个值都会与数字值相比而不是布尔值。

答案 3 :(得分:0)

首先,您写了一些关于您对此功能所需结果的要求的详细信息。我检查了你的功能,它有一些重大的条件问题。

现在,如果你的要求是显示“等级:A”,如果有人在任何类别中获得90分(例如,90美元的天赋或90美元的物理或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);

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

由于

答案 4 :(得分:0)

我aaprreciate“詹姆斯安德森”答案所以+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);

答案 5 :(得分: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";
    }

答案 6 :(得分:0)

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

答案 7 :(得分: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 */