# Random selection from a pool of questions

4 visualizaciones (últimos 30 días)
Laiza Perez el 2 de Dic. de 2020
Comentada: Setsuna Yuuki. el 3 de Dic. de 2020
Hello, I am having a bit of trouble with my code. The purpose is for it to randomly select a question from a pool of questions and the user inputs the answer. The code works to where a question is randomly selected but once the user inputs an answer, it says its incorrect even when the answer is correct. I think the issue is that the questions are being randomized but the answers that match the question are not. How can I fix this?
QandA={'what is 6x8? ', '48';
'what is 12x12? ', '144';
'what is 60/12? ', '5';
'what is 5x6? ', '30';
'what is 8x4? ', '32';
'what is 7x9? ', '63';
'what is 54/9? ', '6';
'what is 2x12? ', '24';
'what is 5x10? ' '50';
'what is 9x3? ', '27';
'what is 22/2? ', '11';
'what is 3x10? ', '30'};
qorder = randperm(numel(QandA(:,1)));
QandAreordered = QandA(qorder, :);
for qidx=1:size(QandAreordered, 1)
if strcmp([answer ' '], QandAreordered{qidx, 2})
fprintf('Correct\n', '%s');
else
fprintf('Incorrect\n', '%s');
end
end
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Setsuna Yuuki. el 2 de Dic. de 2020
Editada: Setsuna Yuuki. el 2 de Dic. de 2020
I changed 2 lines and it works
QandA={'what is 6x8? ', '48';
'what is 12x12? ', '144';
'what is 60/12? ', '5';
'what is 5x6? ', '30';
'what is 8x4? ', '32';
'what is 7x9? ', '63';
'what is 54/9? ', '6';
'what is 2x12? ', '24';
'what is 5x10? ' '50';
'what is 9x3? ', '27';
'what is 22/2? ', '11';
'what is 3x10? ', '30'};
qorder = randperm(numel(QandA(:,1)));
QandAreordered = QandA(qorder, :);
for qidx=1:size(QandAreordered, 1)
if (answer == str2double(QandAreordered{qidx, 2})) %string to double
fprintf('Correct\n', '%s');
else
fprintf('Incorrect\n', '%s');
end
end
##### 2 comentariosMostrar NingunoOcultar Ninguno
Laiza Perez el 3 de Dic. de 2020
what if i wanted to make this a function?
How would I about doing that?
Setsuna Yuuki. el 3 de Dic. de 2020

Iniciar sesión para comentar.

### Categorías

Más información sobre Characters and Strings 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