How to make the question reappear if someone mess up with the number ? eg. 0 to 4. And then someone type 5.
1 次查看(过去 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.
0 个评论
采纳的回答
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 个评论
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 Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!