I need help with a loop.

The program that I am creating needs to go through all of the steps for multiple people until 0 is entered.
For example:
The height and weight will be entered for multiple people and will output multiple BMI's until zero is entered, and then the program will stop, but I don't know how to allow multiple people to input their data.

답변 (2개)

Cedric
Cedric 2013년 2월 27일
편집: Cedric 2013년 2월 27일

0 개 추천

You could start with something like:
personCnt = 0 ;
while true
fprintf('\n--- Person %d\n', personCnt+1) ;
height = input('Enter your height : ') ;
if height == 0, break ; end
weight = input('Enter your weight : ') ;
if weight == 0, break ; end
personCnt = personCnt + 1 ;
% Compute/store/output BMI.
end
Youssef  Khmou
Youssef Khmou 2013년 2월 27일

0 개 추천

hi Anna , there are many ways to control the inputs :
try this :
index=1;
a=rand(1);
b=rand(1);
while (a~=0) ||(b~=0)
fprintf('enter height of individual %i in cm\n',index);
a=input('');
fprintf('enter weight of individual %i in Kg\n',index);
b=input('');
index=index+1;
BMI=rand(1) % You enter the BMI formula here
end
Now you can create matrix A(m,n) to store the inputs for statistical analysis ,

댓글 수: 3

Anna Bradley
Anna Bradley 2013년 2월 27일
Since this is for a class, it is required that we do it the way that our professor wants. She says that our new commands shoud be "two prompt/input to 1-start and 2-continue loop."
Youssef  Khmou
Youssef Khmou 2013년 2월 27일
ok i see, is it a homework? well then complete answer should be not given , only highlights
Youssef  Khmou
Youssef Khmou 2013년 2월 27일
편집: Youssef Khmou 2013년 2월 27일
hi , first i made a mistake using the OR operator , i had to use && instead , you had to be aware of that then, ok you try this enhanced version :
fprintf(' Welcome to Anna''s program : ) \n');
fprintf(' Enter :\n\t\t1.start\n\t\t3.exit\n');
I=input('');
if I==1
index=1;
a=rand(1);
b=rand(1);
while ((a~=0.00) && (b~=0.00))
fprintf('enter height of individual %i in cm\n',index);
a=input('');
fprintf('enter weight of individual %i in Kg\n',index);
b=input('');
index=index+1;
BMI=rand(1) % You enter the BMI formula here
end
fprintf(' Zeros value entered : Program exits');
elseif I==3
;
end

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2013년 2월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by