How to make the question reappear if someone mess up with the number ? eg. 0 to 4. And then someone type 5.

2 ビュー (過去 30 日間)
ques_1 = input('\nIn the last month, how often have you been upset because of something that happened unexpectedly ?\n');
if they answer 5 , how i want to get back to the first question.

採用された回答

Walter Roberson
Walter Roberson 2022 年 1 月 16 日
while true
ques_1 = input('\nIn the last month, how often have you been upset because of something that happened unexpectedly ?\n');
if isnumeric(ques_1) && isscalar(quest_1) && ismember(ques_1, 0:4)
break;
end
end
  5 件のコメント
MUHAMMAD LUQMAN CHE LAH
MUHAMMAD LUQMAN CHE LAH 2022 年 1 月 16 日
Thank you Mr. Roberson, may i know what is the meaning of function isnumeric,isscalar and ismember ?
I have read the documentation and still do not understand it.
Perhaps you can explain it briefly.
Thank you in advance. :')
Walter Roberson
Walter Roberson 2022 年 1 月 16 日
isscalar(EXPRESSION is
isequal(size(EXPRESSION), [1 1])
That is, the expression must have exactly one row and one column (and no further dimensions) -- not a vector, not an array, and not empty. (Empty variables have length 0 in at least one dimension.)
isnumeric(EXPRESSION) is
ismember(class(EXPRESSION), {'single', 'double', 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64'})
In particular, isnumeric() does not include char or string or cell array or struct or logical -- just the built-in numeric datatypes
ismember(EXPRESSION, NUMERIC_LIST) is
OUTPUT = false(size(EXPRESSION));
for K = 1 : numel(EXPRESSION)
if any(EXPRESSION(K) == NUMERIC_LIST(:))
OUTPUT(K) = true;
end
end
So after we have ensured that ques_1 is scalar and numeric, then
ismember(ques_1, 0:4)
would mean the same as
ques_1 == 0 | ques_1 == 1 | ques_1 == 2 | ques_1 == 3 | ques_1 == 4

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by