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
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
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 个评论

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."
ok i see, is it a homework? well then complete answer should be not given , only highlights
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 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by