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 Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by