help me do while function

조회 수: 12 (최근 30일)
THANH NGUYEN
THANH NGUYEN 2013년 2월 26일
Prompt the user to enter a value for height in inches (59-78) and weight in pounds (90-350). I don't know what I can't do that. I enter the height is 50, but it doesn't require me input again. it just show enter the weight.
inches = input('Enter the height in inches (59-78): ');
while (inches <59 && inches>78)
disp('Enter the height in inches (59-78): ');
end
pounds = input('Enter the weight in pounds (90-350): ');
while (pounds <90 && pounds > 350)
pounds = input('Enter the weight in pounds (90-350): ');
end

채택된 답변

Sean de Wolski
Sean de Wolski 2013년 2월 26일
You are yusing && operators indicating that x has to be less than 59 and greater than 78. This isn't possible!
You probably mean to have a ||, the or operator.

추가 답변 (3개)

THANH NGUYEN
THANH NGUYEN 2013년 2월 26일
how about Enter F or M for gender. while gender is not equal to F gender is not equal to M
what is the notation for that? Thanks
  댓글 수: 1
Sean de Wolski
Sean de Wolski 2013년 2월 26일
Use strcmp
doc strcmp
Yopu may also need the ~ operator or not
doc not
E.g:
~strcmp('Sean','F')

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


THANH NGUYEN
THANH NGUYEN 2013년 2월 26일
편집: Walter Roberson 2013년 2월 26일
gender = input('Is the person a female or male? Enter F or M: ','s');
while (gender ~= 'F' || gender ~= 'f' || gender ~= 'M' || gender ~= 'm')
gender = input('Is the person a female or male? Enter F or M: ','s');
end
It keeps asking me enter the input all over and over even I type M,m,f,F. What's wrong with this
  댓글 수: 1
Walter Roberson
Walter Roberson 2013년 2월 26일
If the gender is 'F' then it is not 'M' so the "gender ~= 'M'" part would be true. No matter what you enter, one of the tests is going to be true, and you have used "or" between the parts. Perhaps you should not have used "or".
You should also have used strcmp() like Sean wrote.

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


THANH NGUYEN
THANH NGUYEN 2013년 2월 26일
I don't know how to do it. Can someone give an example to make my dump head understand how to do it? Thanks.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by