problems in the interaction with the user
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hi everyone the code below to ask user to guess a word 'Sol'. The user has three attempts. also has the option to withdraw typing 'Pass'. can you help me make it work?
Sol = 'aaa' %is a char variable
Try = input('insert right world you have 3 trys: ','s')
i = 0;
if strcmpi(Try,'Pass');
P = input('are you sure to esc? [Y/N].\n','s');
if P =='Y'
break;
else
continue;
end
elseif strcmpi(Try,Sol);
fprintf('correct. continue? [Y/N].\n');
if P =='N'
break;
end
else
for i = 0:2
Try = input('...mmm retry: ','s')
if strcmpi(Try,Sol)
P = input('correct. continue? [Y/N].\n','s');
if P =='N'
break;
end
break;
end
P = input('correct. continue? [Y/N].\n','s');
if P =='N'
break;
end
end
end
ps.: any suggestion to optimize the code is welcome
0 Commenti
Risposta accettata
Image Analyst
il 1 Apr 2013
My answer is very very similar to your last question. Just adapted slightly:
Sol = 'aaa' %is a char variable
userPrompt = 'Insert right word. You have 3 tries: ';
counter = 1;
while counter <= 3
userResponse = inputdlg(userPrompt);
if strcmpi(userResponse, Sol)
break;
end
fprintf('Incorrect. Try again.\n');
counter = counter + 1;
end
if counter <= 3
fprintf('Correct.\n');
end
0 Commenti
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!