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

类别

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