필터 지우기
필터 지우기

Judging input word based on first letter

조회 수: 2 (최근 30일)
Andrew Wiebe
Andrew Wiebe 2015년 10월 4일
답변: Walter Roberson 2015년 10월 4일
How would I write an m-file that asks a user to input a word and only returns a correct response if the word begins with "A","B","C".
x=input('Please input a single word surrounded by parentheses.')
case(first letter begins with A,B,C)
disp(Input word is correct.)
case(first letter does not begin with A,B,C)
disp(Input is incorrect. Please input a correct word.)
end
thanks!

답변 (1개)

Walter Roberson
Walter Roberson 2015년 10월 4일
Use the 's' option of input(). Subscript to get the first letter. switch() with case {'A', 'B', 'C'}
Question: what about 'a', 'b', 'c' ?
Watch out for the user pressing return without entering a word

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by