大大幫我寫一個JavaScrip的題目?--20點

選寫一個萬用貸款式算程式,可由輸入貸款金額,利率,償還期來得出每月應繳多少金額?

1 Answer

Rating
  • Ashley
    Lv 7
    1 decade ago
    Favorite Answer

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd%...

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8;" />

    <title>JavaScript 貸款計算機</title>

    <script type="text/javascript">

    function calculate() {

    var principal = document.loandata.principal.value;

    var interest = document.loandata.interest.value / 100 / 12;

    var payments = document.loandata.years.value * 12;

    // 計算每月應付金額

    var x = Math.pow(1 + interest, payments);

    var monthly = (principal*x*interest)/(x-1);

    // 檢查結果是否為有限數,是的話顯示結果

    if (!isNaN(monthly) && (monthly != Number.POSITIVE_INFINITY) && (monthly != Number.NEGATIVE_INFINITY)) {

    document.loandata.payment.value = round(monthly);

    } else { // 輸入無效的話,不顯示

    document.loandata.payment.value = "";

    }

    }

    function round(x) { // 計算到小數點兩位

    return Math.round(x*100)/100;

    }

    </script>

    </head>

    <body>

    <center>

    <form name="loandata" action="">

    <TABLE CELLSPACING=1 CELLPADDING=1>

    <tr><th class=pgtitle colspan="3">JavaScript 貸款計算機</th></tr>

    <tr><td colspan="3"><h2>輸入貸款資訊:</h2></td></tr>

    <tr>

    <td>(1)貸款總金額</td>

    <td><input type="text" name="principal" onchange="calculate();" /></td>

    </tr>

    <tr>

    <td>(2)年利率%</td>

    <td><input type="text" name="interest" onchange="calculate();" /></td>

    </tr>

    <tr>

    <td>(3)償還期(年)</td>

    <td><input type="text" name="years" onchange="calculate();" /></td>

    </tr>

    <tr><td colspan="3"><input type="button" value="計算" onclick="calculate();" /></td></tr>

    <tr><td colspan="3"><h2>繳費資訊:</h2></td></tr>

    <tr>

    <td>(4)每月應繳</td>

    <td><input type="text" name="payment" /></td>

    </tr>

    </table>

    </form>

    </center>

    </body>

    </html>

    Source(s): 個人的程式設計經驗
Still have questions? Get your answers by asking now.