2011-01-09 49 views
-1

我有這個JavaScript公式,我現在正試圖轉換爲PHP。來自JavaScript的PHP公式

的JavaScript:

LVL=new Array(); 
LVL[1]=128; 
LVL[0]=128; 
m=.05; 
for (i=1;i<101;i++) { 
    if (i>1) { 
     LVL[i]=Math.floor(LVL[i-1]+(LVL[i-1]*m)); 
     m=m+.0015; 
    } 
} 

那麼它是一堆的tablefor loopdocument.writes

這裏是我到目前爲止(不工作):

<?php 
$n = 1; // level 
$m = .05; // exp modifier 
$exp = floor($n*1+($n-1)*$m); 
echo "Level " . $n . ", exp needed: " . $exp; // 128 exp 
?> 

PHP的輸出是:Level 1, exp needed: 1,這就是錯誤的。

應該說:Level 1, exp needed: 128

我在做什麼錯?

+0

你忘了for循環... – 2011-01-09 01:11:59

+0

哪有`1 * 1 +(1-1)* 0.05`是`128`?在你的PHP代碼中你沒有事件`128`(而你的JS代碼中有`128`)。你的PHP代碼看起來完全不同... – 2011-01-09 01:12:29

+0

我不需要循環,因爲它只是公式。 – nn2 2011-01-09 01:25:46

回答

1

你做一對夫婦的錯誤:

    您使用索引
  • (中級別),因爲它是 達到該級別所需的經驗點數量。
  • 你忘了的(如果你正在測試公式它是確定)

至今代碼:

$lvl = array(128,128); 
$modifier=.05; 
for ($i=1;$i<101;i++) { 
    $lvl[$i]=floor($lvl[$i-1]*(1+$modifier)); 
    $modifier+=0.0015; 
} 
foreach ($lvl as $level=>$points) { 
    echo "Level " . $level . ", exp needed: " . $points ."\n<br />"; // 128 exp 
} 
2

直接轉錄:

$LVL = array(); 
$LVL[1] = 128; 
$LVL[0] = 128; 
$m = .05; 
for ($i = 1; $i < 101; $i++) 
{ 
    if ($i > 1) 
    { 
     $LVL[$i] = floor($LVL[$i-1] + $LVL[$i-1]*$m); 
     $m = $m + .0015 
    } 
} 

你需要建立數組作爲其內置自下而上