while循环的变量输出不符合预期

时间:2018-11-16 16:12:04

标签: php

我正在尝试编写一小段代码,以检查达到某个级别所需的项目数量,随着级别的增加,每个项目获得的exp数量也会增加,在这种情况下,这就是您的级别* 10。

但是从我尝试过的情况来看,似乎if语句被完全忽略了,我真的不知道如何从这里继续。

这是一段PHP代码:

$currentexp = 0;
$currentlevel = 1;
$nextlevel = 83; //value is experience
$goallevel = 13034431; //value is experience
$itemamount = 0;
$itemexp = $currentlevel * 10;

while ($currentexp < $goallevel) {
    $currentexp = $currentexp + $itemexp;
    $itemamount = $itemamount + 1;
    if ($currentexp > $nextlevel) {
        $nextlevel = $nextlevel * 1.10408986946;
        $currentlevel = $currentlevel + 1;
    }
}

echo "You will need " . $itemamount . " items for your goal level";

给定起点和终点,$ itemamount的输出应该是15000左右。

1 个答案:

答案 0 :(得分:1)

while循环和条件是否被执行。到目前为止,这是我所能知道的。 $itemamount每次迭代增加一。因此与$goallevel差不多。您的循环条件中的变量不会更改,因此每$goallevel

执行一次
    $currentexp = 0;
    $currentlevel = 1;
    $nextlevel = 83; //value is experience
    $goallevel = 13034431; //value is experience
    $itemamount = 0;
    $itemexp = 10;

    while ($currentexp < $goallevel) {
        $currentexp = $currentexp + $itemexp;
        $itemamount = $itemamount + 1;
        if ($currentexp > $nextlevel) {
            $nextlevel = $nextlevel * 1.10408986946;
            $currentlevel = $currentlevel + 1;
            $itemexp = $currentlevel * 10;
        }
    }

    echo "You will need " . $itemamount . " items for your goal level".PHP_EOL;

输出为: 您的目标水平需要11766个项目

更新

以某种方式您的经验计算是错误的。当在Excel工作表中使用固定表时,我得到了正确的值,请参见此示例(我缩短了体验表!):

$currentexp = 0;
$currentlevel = 1;
$goallevel = 13034431; //value is experience
$itemamount = 0;
$itemexp = 10;


$exp_borders = [
    0,
    83,
    174,

    // [...] shortened!

    8771558,
    9684577,
    10692629,
    11805606,
    13034431,
];

while ($currentexp < $goallevel) {
    $currentexp = $currentexp + $itemexp;
    $itemamount = $itemamount + 1;
    if ($currentexp > $exp_borders[$currentlevel]) {
        $currentlevel = $currentlevel + 1;
        $itemexp = $currentlevel * 10;
    }
}

echo "You will need " . $itemamount . " items for your goal level".PHP_EOL;

输出与预期的一样:You will need 15057 items for your goal level