Switch-case problem

6 ビュー (過去 30 日間)
Tiffany McGinnis
Tiffany McGinnis 2019 年 2 月 12 日
回答済み: OCDER 2019 年 2 月 12 日
inches = -2;
while ~(inches >=59 && inches <= 78)
inches = input('Enter the height in inches(59-78) : ');
end
pounds = 0;
while ~(pounds >= 90 && pounds <= 350)
pounds = input('Enter the weight in pounds(90-350) : ');
end
gender = 4;
while ~(gender >=1 && gender <=2)
gender = input('Is this person a female(1) or male(2)? Enter 1 or 2:');
end
%*****COMPUTE*****
% Compute conversions
meters = inches * 0.0254;
kilograms = pounds / 2.2046;
% Compute bmi
bmi= kilograms / ((meters)^2);
switch(gender)
case {'1'}
ibw = (45.5 + 2.3 * (inches - 60));
case {'2'}
ibw = (50.0 + 2.3 * (inches - 60));
end
% Convert IBW to pounds
ibw_in_pounds = ibw * 2.2046;
After running error message:
Undefined function or variable 'ibw'.
Error in assign04a (line 50)
ibw_in_pounds = ibw * 2.2046;

回答 (1 件)

OCDER
OCDER 2019 年 2 月 12 日
ibw wasn't defined if gender is not '1' or '2', which are char. You want gender to be a double of value 1 or 2. Here's a fix.
switch gender
case 1
ibw = (45.5 + 2.3 * (inches - 60));
case 2
ibw = (50.0 + 2.3 * (inches - 60));
otherwise
ibw = %SPECIFY DEFAULT VALUE HERE
error('Incorrect gender option'); %OR, throw an error message
end

カテゴリ

Help Center および File ExchangeProperties についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by