필터 지우기
필터 지우기

my counter isn't working

조회 수: 3 (최근 30일)
Roger L
Roger L 2016년 10월 4일
편집: Matt J 2016년 10월 4일
Hey can you help me figure our why my counter isn't working?
If i input find_consonant('tiger') I need MATLAB to count all of the letters that are not a, e, i, o, u. So the answer should be 3 but my code is not counting right. Thanks for any suggestions.
function count=find_consonant(word)
count=0;
for i=1:length(word);
if word(i)~='a'||word(i)~='e'||word(i)~='i'||word(i)~='o'||word(i)~='u'
count=count+1;
else
count=count;
end
end

채택된 답변

Matt J
Matt J 2016년 10월 4일
편집: Matt J 2016년 10월 4일
Use && instead of | |
  댓글 수: 3
Roger L
Roger L 2016년 10월 4일
Thanks, that worked. I don't see why it would make a difference, the OR comparison make more sense to me in this case. How did you come to that solution?
Matt J
Matt J 2016년 10월 4일
편집: Matt J 2016년 10월 4일
I came to that solution because there were no other candidates. OR will clearly not work, because you are then requiring only that the given letter be different from at least one of the vowels, be it A or E or I or O or U. Any letter will in fact satisfy this. A consonant, however, must be simultaneously different from all of them.

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

추가 답변 (0개)

카테고리

Help CenterFile 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