使用php计算折旧



这是我作为编码初学者第一次在这里提问,所以如果我的问题看起来像是我仍在学习的基本知识,请原谅:(所以我试图使用html和php重新创建这个表单和输出:这是我试图重新创建的代码的屏幕截图

我成功地生成了html和php(我将添加它们以供参考(,但是计算中有一些不完全正确的地方我的html代码:

<!DOCTYPE html>
<html>
<head>
<title>Car Depreciation</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="depreciation.php">
<table>
<tr>
<td>Original Price</td>
<td><input type="text" name="price" size="5"> Dollars</td>
</tr>
<tr>
<td>Residual Value</td>
<td><input type="text" name="residual" size="5"> Dollars</td>
</tr>
<tr><td><input type="submit"></td></tr>
</table>
</form>
</body>
</html>

我的php代码是:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<th>Year</th>
<th>Value at<br>beginning</th>
<th>Annual<br>Depreciation</th>
<th>Accumulated<br>Depreciation</th>
<th>Value at<br>end</th>
</tr>
<?php
//Declaration of variables
$price = $_GET["price"];
$residual = $_GET["residual"];
$accumulateddep = 0;
//Calculations, Loops and Printing
for ($year = 1; $year <= 5; $year++) {
$annualdep = ($price - $residual) / 5;
$accumulateddep+=$annualdep;
$begvalue=$price-$accumulateddep;
$endvalue = $begvalue - $annualdep;
if ($year % 2 == 0)
echo "<tr>
<td>$year</td>
<td>$begvalue</td>
<td>$annualdep</td>
<td>$accumulateddep</td>
<td>$endvalue</td>
</tr>";
else
echo "<tr style='background-color:lightgrey'>
<td>$year</td>
<td>$begvalue</td>
<td>$annualdep</td>
<td>$accumulateddep</td>
<td>$endvalue</td>
</tr>";
}
?>
</table>
</body>
</html>

我的问题是,当我测试它时,我希望第一年的计算使用输入的值,然后从那里继续,然而我的代码所做的是计算第一年的折旧,然后使用该最终值继续(将插入我的输出屏幕截图以供参考(使用我的代码输出的屏幕截图为了准确地固定第一年的折旧计算,需要修改什么?(快速更新:有人提到我应该在文本中说明我的示例值,所以它们是:我使用17000作为原始价格,0.04作为折旧率,所以第一年的计算应该是:17000-3000=14000;3000是根据计算公式计算的年折旧;第二年应该使用第一年的期末值,所以14000-3000=111000,以此类推五年(提前感谢:(

这样就可以了。你需要为行颜色修复它,但应该像预期的一样工作

//Declaration of variables
$price = $_GET["price"];
$residual = $_GET["residual"];
$accumulateddep = 0;
$annualdep = ($price - $residual) / 5;
$accumulateddep=$annualdep;
$endvalue=$price-$annualdep;
//Calculations, Loops and Printing
for ($year = 1; $year <= 5; $year++) {
echo "<tr>
<td>$year</td>
<td>$price</td>
<td>$annualdep</td>
<td>$accumulateddep</td>
<td>$endvalue</td>
</tr>";
$price=$endvalue;
$accumulateddep+=$annualdep;
$endvalue=$price-$annualdep;
}

最新更新