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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我试图进行一个while循环,但循环是无限的,我不确定为什么相关的知识,希望对你有一定的参考价值。

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

<!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>
答案

因为错误

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>

以上是关于我试图进行一个while循环,但循环是无限的,我不确定为什么的主要内容,如果未能解决你的问题,请参考以下文章

结束无限的while循环

while循环中的Javascript continue语句导致无限循环

从无限的while循环中在函数之间传递数据

无限循环与嵌套循环

如何在 SQL Server 中组织无限 while 循环?

在while循环中使用execvp和fork时的无限循环