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)

22 ビュー (過去 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 件のコメント
Image Analyst
Image Analyst 2019 年 2 月 23 日
And if they guessed correctly, it will still say that.
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 Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by