problems in the interaction with the user
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
Image Analyst
2013년 4월 1일
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
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!