How to repeat this loop?

12 vues (au cours des 30 derniers jours)
Karan Sandhu
Karan Sandhu le 12 Mar 2016
Réponse apportée : Jan le 12 Mar 2016
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

Réponse acceptée

Jan
Jan le 12 Mar 2016
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.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by