How to repeat this loop?

14 Ansichten (letzte 30 Tage)
Karan Sandhu
Karan Sandhu am 12 Mär. 2016
Beantwortet: Jan am 12 Mär. 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

Akzeptierte Antwort

Jan
Jan am 12 Mär. 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.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by