User input, for loop printing
조회 수: 2 (최근 30일)
이전 댓글 표시
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);
댓글 수: 0
답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!