Info

此问题已关闭。 请重新打开它进行编辑或回答。

I need to generate a warning statement and I am not exactly sure how.

1 次查看(过去 30 天)
I am having a hard time with the warning statement for this problem.
Says "If the goal is not met in 10 years, a warning should be generated" heres my code
function [yrs, F] = InvestGoal(P,G,i)
%InvestGoal determines the future worth of an investment
%
%INPUT P: the principle amount invested
% G: the goal amount
% i: expected yearly compunded interest
%OUTPUT yrs: number of years needed to reach the goal
% F: the amount the investment is worth at the final year
if numel(P) > 1 || numel(G) > 1 || numel(i) > 1
error('only a single number for each input')
elseif P <= 0 || G <= 0 || i <= 0
error('All inputs must be positive numbers')
elseif G <= P
error('Principle must be less than the Goal amount')
end
disp('Year Future Worth ($)');
A=P;
x=1;
while A < G
A=P*(1+i)^x;
fprintf(' %d %.2f\n',x,A);
x = x + 1;
end
yrs = x - 1;
F = A;
end

回答(2 个)

James Tursa
James Tursa 2015-7-1
E.g., put this at the end of your function:
if( yrs > 10 )
warning('Goal not reached in 10 years');
end

Image Analyst
Image Analyst 2015-7-2
Try
if yrs > 10
message = sprintf('Warning: It has been %d years.\nThe goal was not attained in less than 10 years', yrs);
uiwait(warndlg(message));
end
warndlg() will generate a more user friendly and noticeable popup warning message, and, importantly, wait for the user to click OK before proceeding on with the rest of the code. If you omit uiwait(), then warndlg() will just popup a message and continue on with whatever code follows.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by