我必须创建一个程序,您输入一个数字1-100,它输出与您的分数对应的字母等级,我无法显示提示。这是一些代码:
function myGrade() {
var Input = prompt("Input grade here:");
if (Input >= 90) {
document.write("The score you entered is "
Input ". Your letter grade is A.");
} else if (Input >= 80 && Input < 90) {
document.write("The score you entered is "
Input ". Your letter grade is B.");
} else if (Input >= 70 && Input < 80) {
document.write("The score you entered is "
Input ". Your letter grade is C.");
} else if (Input >= 60 && Input < 70) {
document.write("The score you entered is "
Input ". Your letter grade is D.");
} else if (Input < 60) {
document.write("The score you entered is "
Input ". Your letter grade is F.");
}
}
我在这段代码上面使用<body onload = "myGrade()">
。
答案 0 :(得分:0)
您需要先安装提示:
npm install prompt
所以解决方案比你的解决方案有点复杂:
var prompt = require('prompt');
prompt.start();
prompt.get(['grade'], function (err, Input) {
if (err) { return onErr(err); }
if (Input.grade >= 90) {
console.log("The score you entered is " +
Input.grade + ". Your letter grade is A.");
} else if (Input.grade >= 80 && Input.grade < 90) {
console.log("The score you entered is "
+ Input.grade + ". Your letter grade is B.");
} else if (Input.grade >= 70 && Input.grade < 80) {
console.log("The score you entered is "
+ Input.grade + ". Your letter grade is C.");
} else if (Input.grade >= 60 && Input.grade < 70) {
console.log("The score you entered is "
+ Input.grade + ". Your letter grade is D.");
} else if (Input.grade < 60) {
console.log("The score you entered is "
+ Input.grade + ". Your letter grade is F.");
}
});
function onErr(err) {
console.log(err);
return 1;
}
您可以使用以下命令运行脚本:
node script.js
答案 1 :(得分:0)
这样做:将此代码复制到一个文件中,然后使用浏览器加载该文件。
<!DOCTYPE html>
<html class="">
<head>
<script type = "text/javascript" >
function myGrade() {
var Input = prompt("Input grade here:",50);
if (Input >= 90) {
document.write("The score you entered is "
+ Input + ". Your letter grade is A.");
} else if (Input >= 80 && Input < 90) {
document.write("The score you entered is "
+ Input + ". Your letter grade is B.");
} else if (Input >= 70 && Input < 80) {
document.write("The score you entered is "
+ Input + ". Your letter grade is C.");
} else if (Input >= 60 && Input < 70) {
document.write("The score you entered is "
+ Input + ". Your letter grade is D.");
} else if (Input < 60) {
document.write("The score you entered is "
+ Input + ". Your letter grade is F.");
}
} </script>
</head>
<body onload = "myGrade()">
<br>
</body>
</html>