2014-09-29 74 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循環外定義它們。