How to repeat this loop?
4 ビュー (過去 30 日間)
古いコメントを表示
I have been trying to find a way to get this loop to repeat itself after executing, but it just isn't working. Ideally, when the code finishes running the first time, it should ask for an initial balance again.
LoopRepetition=0;
while LoopRepetition==0
InitialBalance = input('Enter the initial balance in US$ : '); % 10,000,3000,9000
% if initial balance is more than or equal to 5000, then interest rate is
% 2%. Anything else is 1%
if InitialBalance>=5000
rate=0.02;
fprintf('\nYour minimum monthly payment is $%g\n',rate*InitialBalance+1)
Minimum=rate*InitialBalance+1;
else
rate=0.01;
fprintf('\nYour minimum monthly payment is $%g\n',rate*InitialBalance+1)
Minimum=rate*InitialBalance+1;
end
MonthPay = input('Enter the Monthly Payment in US$ : '); % 12000, 4000, 2000
month=0; % initialize months
Payment=0; % initialize payment
Interest=0;
fprintf('\nMonths \t Interest($) \t Payment($) Balance($)\n')
fprintf(' %2i %2i %2i %2i\n',month,Interest,Payment,InitialBalance)
Balance=InitialBalance;
while MonthPay>=Minimum % initiates while loop if and only if monthly payment >= minimum payment
while Balance~=0
month=month+1;
Payment=MonthPay;
Interest=rate*Balance;
Payment=Interest+Balance;
if Balance-Payment<0
Balance=0;
else
Balance=Balance-Payment;
end
fprintf(' %2i %2i %2i %2i\n',month,Interest,Payment,Balance)
end
end
end
0 件のコメント
採用された回答
Jan
2016 年 3 月 12 日
The code you've posted runs in a loop already, as long as it is nocht stopped by Ctrl-C. The loopcondition LoopRepetition==0 is always true, so pleae explain, what you want to change.
It is easier to reconsider your problem, if you post the relevant part of the code only.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!