2015-10-20 52 views
-4

晚安,從基本轉換爲Javascript?

我需要將此示例代碼從 基本轉換爲javascript。 Javascript不支持 「轉到」說明。如果你能給我一個翻譯,我會感謝您的支持。 非常感謝

10 c = 1666.66 
20 b = 1.007897747 
30 a = 10000 
40 n = 6 
50 a = a * b 
60 a = a -c 
70 n = n -1 
80 c = c + 0.01 
90 if a <= 0 then 120 
100 if n = 0 then 30 
110 goto 50 
120 print c 
130 end 
+1

我投票關閉這個問題進行過 - 因爲帖子是要求代碼翻譯。 – Joseph

回答

0

這裏是這樣

// store the information in an Object, so that data can be migrated easily 
 
    var dataSet = setInitialValues(); 
 

 
    // perform the calculation, this is a recursive function 
 
    performCalculations(dataSet); 
 
    
 
    // definition for function that create the initial data set 
 
    function setInitialValues(){ 
 
    \t return { 
 
    \t \t \t c: 1666.66, 
 
    \t \t \t b : 1.007897747, 
 
    \t \t \t a : 10000, 
 
    \t \t \t n : 6 
 
    \t \t } 
 
    } 
 
    
 
    // reassign value to a and n, called when n reached 0 
 
    function reSetValues(d){ 
 
    \t d.a = 10000; 
 
    \t d.n = 6; 
 
    \t return d \t 
 
    } 
 
    
 
    // recursive function, operates on the data set 
 
    function performCalculations(d){ 
 
    \t d.a = d.a*d.b; 
 
    \t d.a = d.a-d.c; 
 
    \t d.n = d.n-1; 
 
    
 
    \t d.c = d.c+0.01; 
 
    \t 
 
    \t if (d.a<= 0){ 
 
    \t \t document.write(d.c) 
 
    \t } else if(d.n==0){ 
 
    \t \t reSetValues(d); 
 
    \t \t performCalculations(d); 
 
    \t } else { 
 
    \t \t performCalculations(d); 
 
    \t } 
 
    }

+0

謝謝,但它不起作用。 Javascrit不運行代碼。 – user5468779

+0

你從哪裏跑這個?將代碼粘貼到瀏覽器的控制檯中並運行它。結果將顯示在控制檯中。對於該記錄,該程序給出的結果是1713.0999999999578 – sunitj

+0

@ user5468779已經更新了片段的答案,您可以看到解決方案正在給出正確的結果。 – sunitj

0

這一建議的一種方式特點相結合的無限循環whilefor環和一個出口。

function x() { 
 
    var a, b, c, n; 
 
    c = 1666.66; 
 
    b = 1.007897747; 
 
    while (true) { 
 
     a = 10000; 
 
     for (n = 6; n--;) { 
 
      a *= b; 
 
      a -= c; 
 
      c += 0.01; 
 
      if (a <= 0) { 
 
       return c; 
 
      } 
 
     } 
 
    } 
 
} 
 
document.write(x());

要證明的結果,您可以複製以下代碼,然後將其插入在線BASIC解釋像http://www.quitebasic.com/

10 let c = 1666.66 
20 let b = 1.007897747 
30 let a = 10000 
40 let n = 6 
50 let a = a * b 
60 let a = a -c 
70 let n = n -1 
80 let c = c + 0.01 
90 if a <= 0 then 120 
100 if n = 0 then 30 
110 goto 50 
120 print c 
130 end