Random selection from a pool of questions

조회 수: 1 (최근 30일)
Laiza Perez
Laiza Perez 2020년 12월 2일
댓글: Setsuna Yuuki. 2020년 12월 3일
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
  댓글 수: 2
Laiza Perez
Laiza Perez 2020년 12월 3일
what if i wanted to make this a function?
How would I about doing that?
Setsuna Yuuki.
Setsuna Yuuki. 2020년 12월 3일
what would be your parameters?

댓글을 달려면 로그인하십시오.

추가 답변 (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