Borrar filtros
Borrar filtros

User input, for loop printing

3 visualizaciones (últimos 30 días)
Cameron
Cameron el 17 de Nov. de 2014
Respondida: Julia el 17 de Nov. de 2014
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);

Respuestas (1)

Julia
Julia el 17 de Nov. de 2014
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.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by