How to repeat this loop?

1 次查看(过去 30 天)
Karan Sandhu
Karan Sandhu 2016-3-12
回答: Jan 2016-3-12
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

采纳的回答

Jan
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 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by