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

2 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 16 de En. de 2022
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 comentarios
MUHAMMAD LUQMAN CHE LAH
MUHAMMAD LUQMAN CHE LAH el 16 de En. de 2022
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 el 16 de En. de 2022
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by