我正在尝试创建一个小网站,从提示中询问用户号码。然后会在div标签上显示一条消息,告诉用户他们获得了什么等级。它不起作用。
例如:如果我得到90%,会出现一条消息,告诉我我的成绩是A +。
以下是代码:
<h1>Prelab1 Ex2</h1><br>
<div id="div2"></div>
<script>
var inputperc = prompt("Enter a percentage");
if(inputperc > 100 || inputperc < 0 || isNaN(inputperc)){
document.getElementById("div2").innerHTML = "Enter a valid Percentage value between 0 and 100";
else{
if(inputperc >= 90 && inputperc <= 100){
document.getElementById("div2").innerHTML = "A+";
}
if(inputperc>90 && inputperc<100){
document.getElementById("div2").innerHTML = "A+";
}
if(inputperc>85 && inputperc<89){
document.getElementById("div2").innerHTML = "A";
}
if(inputperc>80 && inputperc<84){
document.getElementById("div2").innerHTML = "A-";
}
if(inputperc>77 && inputperc<79){
document.getElementById("div2").innerHTML = "B+";
}
if(inputperc>73 && inputperc<76){
document.getElementById("div2").innerHTML = "B";
}
if(inputperc>70 && inputperc<72){
document.getElementById("div2").innerHTML = "B-";
}
if(inputperc>67 && inputperc<69){
document.getElementById("div2").innerHTML = "C+";
}
if(inputperc>63 && inputperc<66){
document.getElementById("div2").innerHTML = "C";
}
if(inputperc>50 && inputperc<62){
document.getElementById("div2").innerHTML = "C-";
}
if(inputperc>57 && inputperc<59){
document.getElementById("div2").innerHTML = "D+";
}
if(inputperc>53 && inputperc<56){
document.getElementById("div2").innerHTML = "D";
}
if(inputperc>50 v&& inputperc<52){
document.getElementById("div2").innerHTML = "D-";
}
if(inputperc>0 && inputperc<49){
document.getElementById("div2").innerHTML = "f";
}
}
}
</script>
答案 0 :(得分:1)
每当用户在输入字段中输入文本时,它都被视为string
。所以你需要将它转换为integer
。提示周围的parseInt
是最简单的方法。 10
只是说你正在使用0,1,2,3,4,5,6,7,8,9(base10)......不像0100100101(base2)。
var inputperc = parseInt(prompt("Enter a percentage"), 10);
答案 1 :(得分:1)
小错误:
...
if(inputperc > 100 || inputperc < 0 || isNaN(inputperc)){
document.getElementById("div2").innerHTML = "Enter a valid Percentage value between 0 and 100";
} else { // <-- forgot to close the if.
if(inputperc >= 90 && inputperc <= 100){
...
同样如前所述,将inputperc
转换为带parseInt()
的数字是个好主意。另外,请确保最后删除额外的}
。
答案 2 :(得分:0)
您的代码有一个语法错误,正如spencer指出的那样。您应该考虑编写getGrade
函数。
var getGrade = function (percent) {
if (percent > 100 || percent < 0 || isNaN(percent)) {
return "Enter a valid Percentage value between 0 and 100";
}
if (percent >= 90 && percent <= 100) return "A+";
if (percent > 90 && percent < 100) return "A+";
if (percent > 85 && percent < 89) return "A";
if (percent > 80 && percent < 84) return "A-";
if (percent > 77 && percent < 79) return "B+";
if (percent > 73 && percent < 76) return "B";
if (percent > 70 && percent < 72) return "B-";
if (percent > 67 && percent < 69) return "C+";
if (percent > 63 && percent < 66) return "C";
if (percent > 50 && percent < 62) return "C-";
if (percent > 57 && percent < 59) return "D+";
if (percent > 53 && percent < 56) return "D";
if (percent > 50 && percent < 52) return "D-";
if (percent > 0 && percent < 49) return "F";
return "Unknown Error"
};
var el = document.getElementById("div2"),
percent = prompt("Enter a percentage:"),
grade = getGrade(percent);
el.innerHTML = grade;