意外的标识符?

时间:2015-06-27 00:22:16

标签: javascript syntax-error

我在这一行收到了一个未检测到的标识符错误:

If userInput <= 7

我真的很陌生,我不明白这意味着什么。我是javascript的新手,但我正在学习。我不知道还有什么可说的,因为我对编程的了解并不是很好。

<html>
<body>
<script type="text/javascript">

   // Declare Constants and Variables
   var stateTax;
   var userInput;
   var finalTax;
   var BR = "</br >"; // Line break
   var ES = " ";  // Empty string
   // Welcome user 
   document.write("Welcome to Sales Tax Calculator" + BR);
   document.write("This program calculates sales tax");
   document.write("under $1.00");
   // take user input
   userInput = Prompt("Please enter a number(Cents)" + ES);

   // call selection structure
   If userInput <= 7 then
   stateTax = 0; 

   else if userInput <= 21 then
   stateTax = 1;

   else if userInput <= 35 then
   stateTax = 2 ;

   else if userInput <= 49 then
   stateTax = 3; 

   else if userInput <= 64 then
   stateTax = 4;

   else if userInput <= 78 then
   stateTax = 5;

   else if userInput <= 92 then
   stateTax = 6;

   else if userInput <= 99 then 
   stateTax = 7;

   else if userInput > 99 then
   document.write("Error, Please enter a value less than 99!");
   end if

   // Calculate and Display sales tax
   finalTax = userInput * stateTax;

   document.write("Sales tax equals: " + finalTax);
   document.write("Thank you for using tax calculator");
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您的代码中存在一些语法问题。您可以做的一件事就是找到问题,然后转到jshint.com并将代码粘贴到其中。然后它会向您显示许多常见错误。一些错误可能会让人感到困惑,但我认为总的来说,它不仅仅会造成伤害。或者,使用具有javascript linting的编辑器,以便它可以防止潜在的错误。

代码中的一些特定语法错误:

  • Prompt()中的“p”必须是小写的(javascript区分大小写)
  • if中的“i”必须是小写的。
  • 您必须将if关键字后面的条件放在括号中(例如if (userInput <= 7) {
  • 您无法使用then标记代码块的开头。而是使用“{”。还可以使用“}”标记代码块的末尾,而不是像endif
  • 这样的关键字

例如,这段代码

If userInput <= 7 then
stateTax = 0; 
else if userInput <= 21 then

应该是这样的

if (userInput <= 7) {
    stateTax = 0;
} else if (userInput <= 21) {

作为旁注,如果条件块中只有一个语句,则括号是可选的,但我强烈建议始终包括括号以避免在以后维护代码时出现问题。此外,你可以使用你想要的任何缩进,我只使用了4个空格,因为我发现这是开源代码中最常见的样式。

相关问题