2014-09-29 42 views
0

我在运行此代码时收到一个NaN错误。我一直在试图找出为什么现在有一段时间,不能为我的生活。JavaScript NaN错误循环

为了澄清,我试图接受;

  • 客户

  • 应付总金额有多少客户在进入

    function Summarise() 
    { 
    
        ID = prompt("Enter a Customer ID number ", -1) 
    
        while(ID != -1) 
        { 
         var gasUsed = 0 
         var total = 0 
         gasUsed = prompt("Enter amount of gas used ", "0") 
         ///Standard Rate is used to assist in the calculation of customers in excess of 60 Gas Used 
         StandardRate = 60 * 1.75 
    
         if(gasUsed <= 60) 
         { 
          total= gasUsed * 2 
          document.write("Customers ID: " + ID) 
          document.write(" has gas usage of " + gasUsed) 
          document.write(" and owes $" + total) 
          document.write("<br/>") 
         } 
         else 
         { 
          total= (gasUsed - 60) * 1.50 + StandardRate 
          document.write("Customers ID: " + ID) 
          document.write(" has gas usage of " + gasUsed) 
          document.write(" and owes $" + total) 
          document.write("<br/>") 
    
         } 
    
         var totalowed = total + totalowed 
         var totalcustomers = ++totalcustomers 
    
         ID = prompt("Enter a Customer ID number ", -1) 
    
        } 
    
        document.write("Total amount owed $" + totalowed) 
        document.write("<br/>") 
        document.write("Total Number of customers:" + totalcustomers) 
    
    
    } 
    
+0

这是因为你使用了一些变量,它是不确定的,并提供明确的值.. – DeDevelopers 2014-09-29 11:50:27

+3

快速提示:分号不(真的)可选。 – 2014-09-29 11:51:59

+0

我还是非常非常新的JavaScript,你能多解释一下@ DeDevelopers – 2014-09-29 11:57:50

回答

0

您需要在循环之前定义这两个变量:

var totalowed = 0; 
var totalcustomers = 0; 

当你分配值给他们,他们最初不确定的,这总是产生问题。

你可以看到下面的工作片段:

function Summarise() { 
 

 
ID = prompt("Enter a Customer ID number ", -1) 
 
var totalowed = 0; 
 
var totalcustomers = 0; 
 
while(ID != -1) 
 
{ 
 
    var gasUsed = 0; 
 
    var total = 0; 
 
    gasUsed = prompt("Enter amount of gas used ", "0"); 
 
    ///Standard Rate is used to assist in the calculation of customers in excess of 60 Gas Used 
 
    StandardRate = 60 * 1.75 
 

 
    if(gasUsed <= 60) 
 
    { 
 
     total = gasUsed * 2; 
 
     document.write("Customers ID: " + ID); 
 
     document.write(" has gas usage of " + gasUsed); 
 
     document.write(" and owes $" + total); 
 
     document.write("<br/>"); 
 
    } 
 
    else 
 
    { 
 
     total= (gasUsed - 60) * 1.50 + StandardRate; 
 
     document.write("Customers ID: " + ID); 
 
     document.write(" has gas usage of " + gasUsed); 
 
     document.write(" and owes $" + total); 
 
     document.write("<br/>"); 
 

 
    } 
 

 
    totalowed = total + totalowed; 
 
    totalcustomers = ++totalcustomers; 
 
    ID = prompt("Enter a Customer ID number ", -1); 
 

 
} 
 

 
document.write("Total amount owed $" + totalowed); 
 
document.write("<br/>") 
 
document.write("Total Number of customers:" + totalcustomers); 
 
} 
 

 
Summarise();

+0

谢谢你,我明白你的意思。愚蠢的错误在我的角色。 – 2014-09-29 12:06:25

+0

我们都有..那里做过的(: – 2014-09-29 12:18:55

-1

你是不是从提示转换字符串转换成整数。在这里看到:

您可以使用:

ID = parseInt(prompt("Enter a Customer ID number ", -1), 10); 

记住要提供一个基数值的parseInt功能的完整性。

How to get numeric value from a prompt box?

0

totalowed和totalcustomers在while循环声明,因此无法使用外面。您将需要在while循环外定义它们。