0

我需要计算 5 个不同的变量。现在这些工作,但我想做的是每增加 50 重新计算价格。现在,我可以在表格中编码,一次最多只能购买 50 个,但我想允许每个人有钱买多少(这就是数字)。所以我认为我需要的是一个抛物线式公式,每 50 次自动增加一次,但我不知道我在寻找什么或如何实现它。

$resprice = 20000+($cityFetch['residential']*502)+($cityNum*1000);
$comprice = 18000+($cityFetch['commercial']*506)+($cityNum*1000);
$indprice = 23000+($cityFetch['industrial']*508)+($cityNum*1000);
$landprice = 600+($cityFetch['land']*.008);
$milprice = 25000+($cityFetch['military']*512)+($cityNum*1000);

编辑:有人指出循环将为此工作。

EDIT3:终于找到了,Havvy 在 IRC 上帮助了我

$cityCheck = mysql_query("SELECT * FROM cities WHERE nation='$nation'");
$cityNum = mysql_num_rows($cityCheck);


function determinePrice($baseCost, $scalar, $current, $bought) {
    $min = $baseCost + $scalar * ($current + 1) + ($cityNum * 1000);
    $max = $baseCost + $scalar * ($current + $bought) + ($cityNum * 1000);
    $average = ($min + $max) / 2;

   return $average * $bought;
}

$resprice = determinePrice(20000, 502, $cityFetch['residential'], $cityFetch['residential']);
$comprice = determinePrice(18000, 506, $cityFetch['commercial'], $cityFetch['commercial']);
$indprice = determinePrice(23000, 508, $cityFetch['industrial'], $cityFetch['industrial']);
$milprice = determinePrice(25000, 502, $cityFetch['residential'], $cityFetch['military']);
$landprice = 600+($cityFetch['land']*.008); 
4

2 回答 2

0

我使用 1 而不是 50 的步长。不根据您一次购买的数量给予折扣更合乎逻辑(少一个幻数),并且对于具有逐步线性函数的新玩家来说会感到困惑。

这个函数背后的原因可以在http://betterexplained.com/articles/techniques-for-adding-the-numbers-1-to-100/找到。

function determinePrice($baseCost, $scalar, $current, $bought) {
    $min = $scalar * ($current + 1) + ($cityNum * 1000);
    $max = $scalar * ($current + $bought) + ($cityNum * 1000);
    $average = ($min + $max) / 2;

   return $average * $bought;
}

使用它看起来像这样:

$resprice = determinePrice(20000, 502, $cityFetch['residential'], $resBought);

但它不适用于土地,因为土地不包括城市价格。对于土地,您可以复制该函数并删除 $cityNum * 1000 部分。您还可以在签名中添加 $isLand 参数,并使用三元运算符添加 0。

如果你不喜欢在声明这个函数之前已经用正确的值声明了 $cityNum,你可以随时将它添加到函数签名中,使其成为一个纯函数。

于 2012-12-21T10:41:53.637 回答
0

$count=1

if($count==50)

{

然后你提高价格

$count=1; }

$resprice = 20000+($cityFetch['residential']*502)+($cityNum*1000);

$comprice = 18000+($cityFetch['commercial']*506)+($cityNum*1000);

$indprice = 23000+($cityFetch['industrial']*508)+($cityNum*1000);

$landprice = 600+($cityFetch['land']*.008);

$milprice = 25000+($cityFetch['military']*512)+($cityNum*1000);

$count++;

于 2012-12-21T10:54:23.407 回答