Javascript 我试图进行一个while循环,但是循环是无限的,我不确定为什么
我正在构建这个代码,测试之后,代码不起作用,它冻结了网页,我认为这是因为一个无限循环。我不确定是什么问题Javascript 我试图进行一个while循环,但是循环是无限的,我不确定为什么,javascript,Javascript,我正在构建这个代码,测试之后,代码不起作用,它冻结了网页,我认为这是因为一个无限循环。我不确定是什么问题 <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Student Loan Payoff</title> <script type="text/javascript"> fu
<!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>
学生贷款偿还
函数DisplayPayoffSchedule(){
var金额、ir、mp、月计数;
amount=parseFloat(document.getElementById('loanBox').value);
ir=parseFloat(document.getElementById('rateBox').value);
mp=parseFloat(document.getElementById('paymentBox').value);
document.getElementById('scheduleDiv')。innerHTML='原始贷款金额:'+amount+'
';
monthcounter=0;
而(数量>mp){
金额=(1+(ir/12))*金额-mp;
monthcounter++;
document.getElementById('scheduleDiv')。innerHTML+='Month'+monthcounter+':剩余金额:'+Amount+'
';
}
}
贷款金额:
年利率:
每月付款:
由于
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;
}
}
}
函数DisplayPayoffSchedule(){
var金额、ir、mp、月计数;
amount=parseFloat(document.getElementById('loanBox').value);
ir=parseFloat(document.getElementById('rateBox').value);
mp=parseFloat(document.getElementById('paymentBox').value);
if(mp<0){
警报(“每月付款必须为正”);
返回;
}
document.getElementById('scheduleDiv')。innerHTML='原始贷款金额:'+amount+'
';
monthcounter=0;
而(数量>mp){
金额=(1+(ir/12))*金额-mp;
monthcounter++;
document.getElementById('scheduleDiv')。innerHTML+='Month'+monthcounter+':剩余金额:'+Amount+'
';
如果(月计数>50){
打破
}
}
}
学生贷款偿还
函数DisplayPayoffSchedule(){
var金额、ir、mp、月计数;
amount=parseFloat(document.getElementById('loanBox').value);
ir=parseFloat(document.getElementById('rateBox').value);
mp=parseFloat(document.getElementById('paymentBox').value);
if(mp<0){
警报(“每月付款必须为正”);
返回;
}
document.getElementById('scheduleDiv')。innerHTML='原始贷款金额:'+amount+'
';
monthcounter=0;
而(数量>mp){
金额=(1+(ir/12))*金额-mp;
monthcounter++;
document.getElementById('scheduleDiv')。innerHTML+='Month'+monthcounter+':剩余金额:'+Amount+'
';
如果(月计数>50){
打破
}
}
}
贷款金额:
年利率:
每月付款:
那么,如何更改语句以使其工作?我是否将条件更改为(amount I updated answer,您添加条件以检查mp<0和return我相信页面仍在无限加载,您是否知道其他替代方法?谢谢您的帮助!如果(monthcounter>50){break;}如果monthcounter太大,您应该使用if(monthcounter>50){break;}来中断循环