필터 지우기
필터 지우기

A code will prompt the user to try to guess my favourite colour and after 3 goes say 3 trials over, but i want to display a message after each incorrect attempt saying "Incorrect try again". How do I do this? (New user)

조회 수: 3 (최근 30일)
This is code i have got so far:
fav = 'red' ;
str = 0 ;
count = 0 ;
while str == 0
count = count+1 ;
prompt = 'What is my favourite color? ';
str2 = input(prompt,'s') ;
if strcmpi(fav,str2)
str = 1 ;
disp('you got the color correct')
elseif count == 3
str = 1 ;
disp('three trials over')
end
end

채택된 답변

Yusuf Azzam
Yusuf Azzam 2019년 2월 25일
I've got it! After 'disp('three trials over')' I add:
else
disp('incorrect')

추가 답변 (1개)

Brian Hart
Brian Hart 2019년 2월 23일
add the code
disp('Incorrect try again')
right before your very last end statement
  댓글 수: 2
Yusuf Azzam
Yusuf Azzam 2019년 2월 25일
Yes, when I added it it still says incorrect try again when they guess correctly AND after they run out of chances, this is not what i want. Is there another way?

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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by