我是初学者,我需要一些帮助。我无法弄清楚我做错了什么。
我必须制作一个程序来读取从0到100的值的检查分数集合,并停止处理-1。输入并验证分数。程序应计算并打印通过次数(> = 50)和失败次数(0-50)。输入分数-1时,将显示通过次数和失败次数。
<script>
var score = 0;
var passCount = 0;
var failCount = 0;
score = parseInt(prompt("Input score between 1-100, -1 to quit","0"));
while (score !< 0){
if (score >= 50 || score <= 100){
passCount = passCount + 1;
alert ("You passed! Pass count = "+passCount+"Fail count = "+failCount);
}
else
if (score<50){
failCount = failCount + 1;
alert ("You failed! Pass count = "+passCount+"Fail count = "+failCount);
}
else
if (score > 100){
alert ("Invalid number");
}
score = parseInt(prompt("Input score between 1-100, -1 to quit","0"));
}
document.write ("Total: Passes - "+passCount+"Fails "+failCount);
</script>
答案 0 :(得分:1)
要完成的更改:
while (score >= 0)
没有运营商!<
。您必须使用while (! (score<0))
或while (score >=0)
或while (score != -1)
if(得分> = 50 &&
得分&lt; = 100)
由于您在原始代码中使用||
OR
运算符,因此控件永远不会转到failCount分支。需要使用AND
运算符才能使条件有效。
最终代码:
<script>
var score = 0;
var passCount = 0;
var failCount = 0;
score = parseInt(prompt("Input score between 1-100, -1 to quit","0"));
while (score >= 0){
if (score >= 50 && score <= 100){
passCount = passCount + 1;
alert ("You passed! Pass count = "+passCount+"Fail count = "+failCount);
}
else
if (score<50){
failCount = failCount + 1;
alert ("You failed! Pass count = "+passCount+"Fail count = "+failCount);
}
else
if (score > 100){
alert ("Invalid number");
}
score = parseInt(prompt("Input score between 1-100, -1 to quit","0"));
}
document.write ("Total: Passes - "+passCount+"Fails "+failCount);
</script>
答案 1 :(得分:0)
尝试更改条件while(score != -1)