Random selection from a pool of questions

1 次查看(过去 30 天)
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)
answer=input(QandAreordered{qidx, 1}, 's');
if strcmp([answer ' '], QandAreordered{qidx, 2})
fprintf('Correct\n', '%s');
else
fprintf('Incorrect\n', '%s');
end
end

采纳的回答

Setsuna Yuuki.
Setsuna Yuuki. 2020-12-2
编辑:Setsuna Yuuki. 2020-12-2
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)
answer=input(QandAreordered{qidx, 1}); %double
if (answer == str2double(QandAreordered{qidx, 2})) %string to double
fprintf('Correct\n', '%s');
else
fprintf('Incorrect\n', '%s');
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by