Help me do a big while function

15 Ansichten (letzte 30 Tage)
THANH NGUYEN
THANH NGUYEN am 28 Feb. 2013
Prompt the user to enter a value for height in inches (59-78). Enter zero to end.
While height not equal to zero process BMI information
Prompt the user to enter a value for weight in pounds (90-350).
For example, when run the program, it asks me to input the height in the range above. If I enter 0, the program stop. If not zero, it will start compute the bmi with the condition I created in a first part. My instructor said I have to do a big WhiLE, BUT i DON'T KNOW HOW TO DO IT. pLEASE HELP ME. tHANKS.
  2 Kommentare
Sean de Wolski
Sean de Wolski am 28 Feb. 2013
As opposed to a little while?
I typically tell me wife I'll be home in a little while; I suppose she probably interprets this as a big while.
Walter Roberson
Walter Roberson am 28 Feb. 2013
Sounds like you didn't license the GOTO Toolbox.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

THANH NGUYEN
THANH NGUYEN am 28 Feb. 2013
Bearbeitet: Walter Roberson am 28 Feb. 2013
I add a while above of the first line of my code, but it doesn't work as I want. The below is the code for first part. The first 2 while I use or. The last while I use and. Now I have to adjust a little bit but still don't know how to do
% Prompt the user to enter a value for weight in pounds and height in inches
inches = input('Enter the height in inches (59-78): ');
while (inches <59 || inches>78)
inches=input('Enter the height in inches (59-78): ');
end
pounds = input('Enter the weight in pounds (90-350): ');
while (pounds <90 || pounds > 350)
pounds=input('Enter the weight in pounds (90-350): ');
end
% === BMI ======
% compute converstions
meters = inches * 0.0254;
kilograms = pounds / 2.2046;
% calculate BMI
bmi = kilograms / meters ^ 2;
% display BMI
fprintf('The BMI is: %.2f\n', bmi);
disp(' ')
% display BMI classification
fprintf('BMI Classification: ');
if ( bmi < 18.5)
fprintf('Underweight\n')
elseif (bmi >= 18.5 && bmi < 25)
fprintf('Normal\n')
elseif (bmi >= 25 && bmi < 30)
fprintf('Overweight\n')
else
fprintf('Obese\n')
end
% === TARGET BMI AND WEIGHT ======
% Prompt the user to enter a value for BMI
target_BMI = input('Enter the target BMI (19-25): ');
while (target_BMI <19 || target_BMI >25);
target_BMI= input('Enter the target BMI (19-25): ');
end
% compute weight
kilograms = target_BMI * meters^2;
% Compute conversions
pounds = 2.2046 * kilograms;
% display weight
fprintf('The target weight is: %.1f pounds\n\n', pounds)
% === IBW ======
%Prompt user to enter the gender (F or M)
gender = input('Is the person a female or male? Enter F or M: ','s');
while (gender ~= 'F' && gender ~= 'f' && gender ~= 'M' && gender ~= 'm')
gender = input('Is the person a female or male? Enter F or M: ','s');
end
% compute the IBW for the given height and gender
switch (gender)
case {'F','f'}
ideal = 45.5 + 2.3 * (inches-60);
case {'M','m'}
ideal = 50.0 + 2.3 * (inches-60);
end
% Convert IBW to pounds
pounds = 2.2046 * ideal;
% display IBW
fprintf('The IBW is: %.1f pounds\n\n', pounds);
end

Walter Roberson
Walter Roberson am 28 Feb. 2013

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by