有没有办法在 JavaScript 中使用带有变量的 switch 语句来显示基于另一个变量的文本
我正在尝试获取一个 switch 语句来使用名为“avg”的变量来表示五个成绩的平均值。将五个等级相加得到总分,然后除以 5 得到平均值。像这样:
avg = +grade_one + +grade_two + +grade_three + +grade_four + +grade_five;
开关需要显示五种情况,每个等级范围一种(即 90% 到 100% = A)。以及百分比本身。
我尝试过使用具有数值的案例,例如“案例 1”,然后打印两行文本,但这不起作用。
我应该在切换之前尝试使用 if-else 结构来确定字母中每个成绩百分比的相等值,还是行不通?
这是开关的代码。
switch (avg)
{
case 1:
letter = "A";
per = "90-100";
document.write("Based on your semester average, your grade
falls between " + per + "%");
document.write("you earned a " + letter);
break;
case 2:
letter = "B";
per = "80-89.9";
document.write("Based on your semester average your grade falls between " + per + "%");
document.write("you earned a " + letter);
break;
case 3:
letter = "A";
per = "70-79.9";
document.write("Based on your semester average your grade falls between " + per + "%");
document.write("you earned a " + letter);
break;
case 4:
letter = "D";
per = "60-69.9";
document.write("Based on your semester average your grade falls between " + per + "%");
document.write("you earned a " + letter);
break;
case 5:
letter = "F";
per = "Below 60%";
document.write("Based on your semester average your grade falls between " + per + "%");
document.write("you earned a " + letter);
default:
document.write("Invalid Entry");
break;
}
I am trying to get a switch statement to use a variable called "avg" for the average of five grades. The five grades are added together to get the total and then divided by five to get the average. Like this:
avg = +grade_one + +grade_two + +grade_three + +grade_four + +grade_five;
The switch needs to display five cases, one for each grade range, (i.e 90% to 100% = A). As well as the percentage itself.
I have tried using cases with numeric values like "case 1" for example, and then printing two lines of text but that won't work.
Should I try using an if-else structure before the switch to establish what each grade percentage equals in a letter, or will that not work?
Here is the code for the switch.
switch (avg)
{
case 1:
letter = "A";
per = "90-100";
document.write("Based on your semester average, your grade
falls between " + per + "%");
document.write("you earned a " + letter);
break;
case 2:
letter = "B";
per = "80-89.9";
document.write("Based on your semester average your grade falls between " + per + "%");
document.write("you earned a " + letter);
break;
case 3:
letter = "A";
per = "70-79.9";
document.write("Based on your semester average your grade falls between " + per + "%");
document.write("you earned a " + letter);
break;
case 4:
letter = "D";
per = "60-69.9";
document.write("Based on your semester average your grade falls between " + per + "%");
document.write("you earned a " + letter);
break;
case 5:
letter = "F";
per = "Below 60%";
document.write("Based on your semester average your grade falls between " + per + "%");
document.write("you earned a " + letter);
default:
document.write("Invalid Entry");
break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该为此使用
switch
:每种情况的代码都太相似。区别仅在于几个值,您可以将它们存储在数组中。然后在该数组中找到平均值落在哪个段,并从中构建输出。另外,不要为此使用
document.write
。相反,创建一个包含输出消息的元素(如div
)。我不知道你的练习的背景是什么,但我想有一些 5 级的输入。将它们加载为数组(而不是 5 个不同的变量),并以通用方式计算平均值(不绑定到 5)。
以下是可运行片段中的一些建议代码:
You should not use a
switch
for that: the code for each case is too similar. The difference is just in a few values, which you can store in an array. Then find in that array in which segment the average falls, and build the output from that.Also, don't use
document.write
for this. Instead create an element (like adiv
) that will contain the output message.I don't know what the context is of your exercise, but I suppose there are some inputs with the 5 grades. Load those as an array (not as 5 distinct variables), and calculate the average in a generic way (not tied to 5).
Here is some suggested code in a runnable snippet: