我试图进行一个while循环,但循环是无限的,我不确定为什么

问题描述 投票:-1回答:1

我正在构建这个代码并在测试之后,代码不起作用并冻结了网页,我认为这是因为无限循环。我不确定问题是什么。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>Student Loan Payoff</title>
        <script type="text/javascript">
function DisplayPayoffSchedule() {
    var amount, ir, mp, monthcounter;

    amount = parseFloat(document.getElementById('loanBox').value);
    ir = parseFloat(document.getElementById('rateBox').value);
    mp = parseFloat(document.getElementById('paymentBox').value);

    document.getElementById('scheduleDiv').innerHTML = 'Original loan amount: ' + amount + '<br>';

    monthcounter = 0;

    while (amount > mp) {
        amount = (1 + (ir / 12)) * amount - mp;
        monthcounter++;
        document.getElementById('scheduleDiv').innerHTML += ' Month ' + monthcounter + ': Amount Remaining : ' + amount + '<br>';
    }

}
        </script>
    </head>
<body>
    <p>
Amount of Loan: <input type="text" id="loanBox" size="6"><br>
Annual Interest Rate: <input type="text" id="rateBox" size="6"><br>
Monthly Payment: <input type="text" id="paymentBox" size="6">
    </p>
        <input type="button" value="Display Payoff Schedule"  onclick="DisplayPayoffSchedule();">
    <hr>
        <div id="scheduleDiv"></div>
</body>
</html>
javascript
1个回答
1
投票

因为错误

amount = (1 + (ir / 12)) * amount - mp;

如果您输入的每月付款为负数,则金额大于mp且无限循环。

应防止输入负数。

当月份计数器太大时也应该打破。

if(monthcounter > 50){
    break;
}

因为如果贷款金额太大而且利率和每月付款非常小,它将重复很多循环并且看起来像无限循环。

function DisplayPayoffSchedule() {
    var amount, ir, mp, monthcounter;

    amount = parseFloat(document.getElementById('loanBox').value);
    ir = parseFloat(document.getElementById('rateBox').value);
    mp = parseFloat(document.getElementById('paymentBox').value);

    if(mp < 0){
    alert('Monthly Payment must be positive');
    return;
    }

    document.getElementById('scheduleDiv').innerHTML = 'Original loan amount: ' + amount + '<br>';

    monthcounter = 0;

    while (amount > mp) {
        amount = (1 + (ir / 12)) * amount - mp;
        monthcounter++;
        document.getElementById('scheduleDiv').innerHTML += ' Month ' + monthcounter + ': Amount Remaining : ' + amount + '<br>';
        if(monthcounter > 50){
           break;
        }
    }

}

<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>Student Loan Payoff</title>
        <script type="text/javascript">
function DisplayPayoffSchedule() {
    var amount, ir, mp, monthcounter;

    amount = parseFloat(document.getElementById('loanBox').value);
    ir = parseFloat(document.getElementById('rateBox').value);
    mp = parseFloat(document.getElementById('paymentBox').value);
	
	if(mp < 0){
	alert('Monthly Payment must be positive');
	return;
	}

    document.getElementById('scheduleDiv').innerHTML = 'Original loan amount: ' + amount + '<br>';

    monthcounter = 0;

    while (amount > mp) {
        amount = (1 + (ir / 12)) * amount - mp;
        monthcounter++;
        document.getElementById('scheduleDiv').innerHTML += ' Month ' + monthcounter + ': Amount Remaining : ' + amount + '<br>';
        if(monthcounter > 50){
           break;
        }
    }

}
        </script>
    </head>
<body>
    <p>
Amount of Loan: <input type="text" id="loanBox" size="6"><br>
Annual Interest Rate: <input type="text" id="rateBox" size="6"><br>
Monthly Payment: <input type="text" id="paymentBox" size="6">
    </p>
        <input type="button" value="Display Payoff Schedule"  onclick="DisplayPayoffSchedule();" />
    <hr>
        <div id="scheduleDiv"></div>
</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.