how to prompt a user to enter a string, then check that the entered string is one of the acceptable entries. If it is not, user must enter it again.
6 ビュー (過去 30 日間)
古いコメントを表示
I am trying to prompt the user to input a unit for speed. I have tried while loops, if/elseif/else, but I cannot get it to work correctly.
Here is an example of a while loop that I tried. I have tried many other ways but I cannot seem to get it to work. If I use an if/elseif/else approach, it will deny an incorrect entry two times before it just lets the user continue. However, it correctly detected when a proper unit is entered. I would like to know how to indefinitely stop the user if they do not enter one of the correct units, and then let them continue once a correct unit is entered.
unit = input('TEXT PROMPTING USER TO INPUT UNITS (EXAMPLE OF ACCEPTED UNITS (unit q, unit w, unit e, unit r) ', 's');
while unit ~= strcmpi(unit, 'q') || strcmpi(unit, 'w') || strcmpi(unit, 'e') || strcmpi(unit, 'r')
unit = input('ERROR MESSAGE IF UNACCEPTED UNIT IS ENTERED (DISPLAYING ACCEPTED UNITS AGAIN: ', 's');
end
0 件のコメント
採用された回答
the cyclist
2021 年 3 月 19 日
編集済み: the cyclist
2021 年 3 月 19 日
I think this is what you intended ...
unit = input('TEXT PROMPTING USER TO INPUT UNITS (EXAMPLE OF ACCEPTED UNITS (unit q, unit w, unit e, unit r) ', 's');
while not(strcmpi(unit, 'q') || strcmpi(unit, 'w') || strcmpi(unit, 'e') || strcmpi(unit, 'r'))
unit = input('ERROR MESSAGE IF UNACCEPTED UNIT IS ENTERED (DISPLAYING ACCEPTED UNITS AGAIN: ', 's');
end
rather than checking that the value of unit was equal to that series of logical statements.
その他の回答 (1 件)
Walter Roberson
2021 年 3 月 19 日
unit = input('TEXT PROMPTING USER TO INPUT UNITS (EXAMPLE OF ACCEPTED UNITS (unit q, unit w, unit e, unit r) ', 's');
while ~ismember(lower(unit), {'q', 'w', 'e', 'r'})
unit = input('ERROR MESSAGE IF UNACCEPTED UNIT IS ENTERED (DISPLAYING ACCEPTED UNITS AGAIN: ', 's');
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Get Started with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!