【问题标题】:Calculating Depreciation using php使用php计算折旧
【发布时间】:2021-08-20 10:33:43
【问题描述】:

这是我作为编码初学者第一次在这里提问,如果我的问题看起来像是我仍在学习的基本知识,请原谅我 :) 所以我试图重新创建这个表单并使用 html 和 php 输出: this is the screenshot of the code I was trying to recreate

并且我成功地生成了 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>

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

【问题讨论】:

  • 请向我们提供输入值和预期输出值的示例,以及您获得的当前输出值作为文本,然后我们可以测试并重新使用它们。谢谢。
  • 您需要确定第一次迭代(这应该不难,因为那只是第 1 年),然后设置 $begvalue=$price 如果它是第一个,否则设置 $begvalue=$price-$accumulateddep;。只需一个简单的if 条件(或三元表达式)。
  • 折旧只是(初值-终值)/年。这是每年的折旧。 (14.000 - 2.000)/5=12.000/5 这不是像您在图片中显示的 3000
  • @El_Vanja 你能进一步澄清一下吗?
  • $annualdep = ($price - $residual) / 5; 也应该不在 for 循环中。你必须计算一次,然后每年都一样

标签: php html loops if-statement


【解决方案1】:

这应该可以解决问题。您需要为行颜色修复它,但应该按预期工作

//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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多