User input, for loop printing

2 ビュー (過去 30 日間)
Cameron
Cameron 2014 年 11 月 17 日
回答済み: Julia 2014 年 11 月 17 日
How do I make the for loops add my values to user_dia_prcnt and display it? The current code will only print out 0
clear all
clear clf
clear clc
user_dia_prcnt = 0;
user_age = input('Type your age: ');
user_gender = input('Type M or F: ','s');
%Gathers BMI
user_weight = input('Type your weight (lbs): ');
user_height = input('Type your height (in): ');
user_bmi = (user_weight*703)/user_height^2;
%add fprintf to final summary
fprintf('Your Body Mass Index is %f\n', user_bmi);
if(user_gender == 'M')
if(user_age <= 45)
if(user_bmi >= 35)
user_dia_prcnt + 66.1;
if(user_bmi <= 35)
user_dia_prcnt + 51.8;
if(user_bmi <= 30)
user_dia_prcnt + 25.5;
if(user_bmi <= 25)
user_dia_prcnt + 16.9;
else
user_dia_prcnt + 6.2;
end
end
end
end
end
end
fprintf('Percentage is: %f \n ',user_dia_prcnt);

回答 (1 件)

Julia
Julia 2014 年 11 月 17 日
Hi,
this code works:
clear all
clear clf
clear clc
user_dia_prcnt = 0;
user_age = input('Type your age: ');
user_gender = input('Type M or F: ','s');
%Gathers BMI
user_weight = input('Type your weight (lbs): ');
user_height = input('Type your height (in): ');
user_bmi = (user_weight*703)/user_height^2;
%add fprintf to final summary
fprintf('Your Body Mass Index is %f\n', user_bmi);
if strcmp(user_gender, 'M')
if(user_age <= 45)
if(user_bmi >= 35)
user_dia_prcnt = user_dia_prcnt + 66.1;
elseif(user_bmi <= 25)
user_dia_prcnt = user_dia_prcnt + 51.8;
elseif(user_bmi <= 30)
user_dia_prcnt = user_dia_prcnt + 25.5;
elseif(user_bmi <= 35)
user_dia_prcnt = user_dia_prcnt + 16.9;
else
user_dia_prcnt = user_dia_prcnt + 6.2;
end
end
end
fprintf('Percentage is: %f \n ',user_dia_prcnt);
You don't specify what happens, if the user is female. And I think other cases are missing as well.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by