2015-10-17 67 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