Case 语句输出不正确?

发布于 2024-12-13 04:08:46 字数 609 浏览 2 评论 0原文

这很奇怪,但下面是我的案例陈述:

switch($grade){
    case ($average >70):
    $grade = 'A';
    break;
    case ($average >=60 && $average <=69):
    $grade = 'B';
    break;
    case ($average >=50 && $average <=59):
    $grade = 'C';
    break;

};

所以如果它的 70+ 是 A 级,60-69 B 级,50-59 C 级。

但它输出的是:60+ A 级,50-59 B 级, 40-49 C 级。

为什么要这样做,因为功能看起来是正确的?

    echo "<p><strong>Average Mark:</strong> $average</p>";
    echo "<p><strong>Average Grade:</strong> $grade</p>";

This is very strange but below is my case statement:

switch($grade){
    case ($average >70):
    $grade = 'A';
    break;
    case ($average >=60 && $average <=69):
    $grade = 'B';
    break;
    case ($average >=50 && $average <=59):
    $grade = 'C';
    break;

};

So if its 70+ it is grade A, 60-69 grade B, 50-59 grade C.

But instead it outputting this: 60+ grade A, 50-59 grade B, 40-49 grade C.

Why is it doing this because function seems correct?

    echo "<p><strong>Average Mark:</strong> $average</p>";
    echo "<p><strong>Average Grade:</strong> $grade</p>";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

面犯桃花 2024-12-20 04:08:46

正如其他人在评论中提到的,案例中的“条件”应该是静态值,而不是逻辑表达式。

此外,您要打开的值(在您的情况下为 $grade)应该是您正在测试的值。您似乎正在使用它作为有关您要分配的变量的提示。

修复代码的最简单方法是使用 if-elseif-else 构造:

if ($average >70)
    $grade = 'A';
elseif ($average >=60 && $average <=69)
    $grade = 'B';
elseif ($average >=50 && $average <=59)
    $grade = 'C';

但是,为了说明 switch 语句的工作原理,您还可以执行以下操作:

switch(true){
    case ($average >70):
        $grade = 'A';
        break;
    case ($average >=60 && $average <=69):
        $grade = 'B';
        break;
    case ($average >=50 && $average <=59):
        $grade = 'C';
        break;
};

在本例中,我正在比较值依次为每个情况 true,其中每个情况值实际上是评估布尔表达式的结果。第一个值与 true 匹配的表达式将被触发。

如果您不理解 switch 语句,可能没有多大帮助。

编辑:我刚刚注意到逻辑上存在差距:如果某人的平均分恰好是 70 该怎么办?使用诸如 switch 或 if-else 之类的级联语句,您可以消除一些冗余(在本例中是破坏性的)代码,从而:

if ($average >=70)
    $grade = 'A';
elseif ($average >=60)
    $grade = 'B';
elseif ($average >=50)
    $grade = 'C';
// ...
else
    $grade = 'F';

...等等,直到您使用的最低等级。

As others mentioned in comments, the "condition" in a case should be a static value, not a logical expression.

Also, the value you're switching on (in your case, $grade) should is the one you're testing. You appear to be using it as a hint about what variable you're assigning.

The simplest way to fix your code would be to use an if-elseif-else construct:

if ($average >70)
    $grade = 'A';
elseif ($average >=60 && $average <=69)
    $grade = 'B';
elseif ($average >=50 && $average <=59)
    $grade = 'C';

However, to be perverse, and to illustrate how a switch statement works, you could also do the following:

switch(true){
    case ($average >70):
        $grade = 'A';
        break;
    case ($average >=60 && $average <=69):
        $grade = 'B';
        break;
    case ($average >=50 && $average <=59):
        $grade = 'C';
        break;
};

In this example I'm comparing the value true to each of the cases in turn, where each of those case-values is actually the result of evaluating a boolean expression. The first expression whose value matches true will fire.

Probably not much help, if you don't understand switch statements.

Edit: I just noticed that there's a gap in the logic: what if someone's average is exactly 70? Using a cascading statement like a switch or if-else, you can eliminate some of the redundant (and in this case damaging) code, thus:

if ($average >=70)
    $grade = 'A';
elseif ($average >=60)
    $grade = 'B';
elseif ($average >=50)
    $grade = 'C';
// ...
else
    $grade = 'F';

...and so on, to whatever lowest grade you're using.

娇俏 2024-12-20 04:08:46

对于那些说切换变量$average的人来说,你错了。它在该实例中进行评估的唯一原因是因为 switch 使用松散比较,因此它表示 $average being set 为 true 并进行比较它到条件句,所有条件要么为真,要么为假。以前,使用未设置的 $grade 会将开关评估为 false,因为在松散比较中,未设置的变量将引发通知并返回 false。

虽然我建议使用 if-then-else,但在这种情况下使用 switch 语句的正确答案如下:

switch (true) {
    case ($average >= 70):
        $grade = 'A';
        break;
    case ($average >= 60 && $average < 70):
        $grade = 'B';
        break;
    case ($average >= 50 && $average < 60):
        $grade = 'C';
        break;
}

如上所述,每个语句将返回 true 或错误的。这个想法是,一次只有一个语句应该返回 true,因此 switch 语句会将其 true 值与传递的一个语句匹配并仅执行该代码,因为所有其他语句都是 false 并且不匹配。

For those of you saying switch the variable you $average, you are wrong. The only reason it is evaluating in that instance is because switch uses loose comparison, so it is saying that $average being set is true and comparing it to the conditionals, all of which will be either true or false. Previously, using $grade which was unset was evaluating the switch to false because in loose comparison, a variable which is unset will throw a notice and return false.

While I recommend using if-then-else, the proper answer for using a switch statement in this case is as follows:

switch (true) {
    case ($average >= 70):
        $grade = 'A';
        break;
    case ($average >= 60 && $average < 70):
        $grade = 'B';
        break;
    case ($average >= 50 && $average < 60):
        $grade = 'C';
        break;
}

Like said above, every statement will return either true or false. The idea is that only one statement should ever return true at one time, thus the switch statement will match it's value of true to the one statement that passed and execute that code only, since all of the other ones are false and didn't match.

葬心 2024-12-20 04:08:46

您应该使用 if/else - 语句:

if($average >70)
{
   $grade = 'A';
} else if($average >=60 && $average <=69)
{
   $grade = 'B';
} else if($average >=50 && $average <=59)
{
   $grade = 'C';
}

You should use if/else - statements:

if($average >70)
{
   $grade = 'A';
} else if($average >=60 && $average <=69)
{
   $grade = 'B';
} else if($average >=50 && $average <=59)
{
   $grade = 'C';
}
我不会写诗 2024-12-20 04:08:46

编辑: 或者,您可以计算 $grade 值(本例中为 50-100):

$grades = "CBAAA";
$grade = $grades[(int)($average/10) - 5];

switch 循环比较案例。如果您确实想使用 switch 来完成这项工作(而不是 if / elseif / else),那么您可以使用与 TRUE 进行比较的 switch 来实现:

switch(TRUE)
{
    case $average > 70:
        $grade = 'A';
        break;

    case $average >= 60 && $average <= 69:
        $grade = 'B';
        break;

    case $average >= 50 && $average <= 59:
        $grade = 'C';
        break;

    default:
        throw new Exception(sprintf('Unable to map average (%d) to a grade.', $average));
}

Edit: Alternatively you can calculate the $grade value (50-100 in this example):

$grades = "CBAAA";
$grade = $grades[(int)($average/10) - 5];

The switch loop compares the cases. If you really want to use switch for the job (and not if / elseif / else), then you can to it with a switch that compares against TRUE:

switch(TRUE)
{
    case $average > 70:
        $grade = 'A';
        break;

    case $average >= 60 && $average <= 69:
        $grade = 'B';
        break;

    case $average >= 50 && $average <= 59:
        $grade = 'C';
        break;

    default:
        throw new Exception(sprintf('Unable to map average (%d) to a grade.', $average));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文