2015-10-17 69 views
0

我必须创建一个程序,您可以在其中输入数字1-100,并输出与您的分数相对应的字母等级,并且无法显示提示。以下是一些代码:HTML提示()将不会显示

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

你在控制台上看到了什么错误? – 2015-10-17 05:39:45

回答

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> 
+0

问题是什么,你有什么改变? – 2015-10-17 05:39:20

0

您需要安装提示第一:

npm install prompt 

因此,解决办法是一点点比你复杂:

node script.js 

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; 
} 

您可以使用此命令运行脚本

More information

+0

'npm prompt'是用于提示节点应用程序从命令行输入的东西。此问题未被标记[tag:nodejs]。我认为OP正在浏览器中运行他的程序。 – 2015-10-17 05:42:08