how can i write a matlab code to a multple choice question having multiple answers?? say i have to pick out A,B,C,E but excluding D. so far i have this below...

조회 수: 8 (최근 30일)
n=[A,B,C,E];
for n=input('Enter your answers:');
switch (n);
case 'D'; case 'd';
if n=='D'||n=='d'
disp('wrong answer. Try again please');
fprintf('Your scored mark is zero(0)\n');
break;
end
if n=='A'||n=='a'&& n=='B'||n=='b'&& n=='C'||n=='c'&& n=='E'||n=='e';
disp('correct answers,congratulations!');
break;
end
end
end
  댓글 수: 2
Guillaume
Guillaume 2015년 7월 1일
Note that
if n=='A'&& n=='B' ... (the rest does not matter)
will never be true. If n is equal to 'A' it is certainly not equal to 'B'. You probably meant to have || everywhere.
enoch adotey
enoch adotey 2015년 7월 2일
thank you much for your correction, am looking through to make my changes

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

채택된 답변

Nick
Nick 2015년 7월 1일
편집: Nick 2015년 7월 1일
answer = 'n'
while(answer ~= 'd')
in = input('Get input: ','s');
switch lower(in)
case 'a'
disp('Right')
answer = 'a';
case 'b'
disp('Right')
answer = 'b';
case 'c'
disp('Right')
answer = 'c';
case 'd'
disp('Wrong')
answer = 'd';
case 'e'
disp('Right')
answer = 'e';
otherwise
disp('Invalid choice')
answer = 'na';
end
end
You can put a function in the correct cases instead of the disp('Right')answer ='a' so it runs the same function if you wanted to as well
  댓글 수: 2
Joseph Cheng
Joseph Cheng 2015년 7월 1일
or you can use logical operands as you are hard coding the answers in or use strmatch or strncmp to check strings
posAns = 'abcde';
n=sort(lower(input('Enter your answers:','s')));
Uanswer = zeros(size(posAns));
for ind = 1:numel(posAns)
Uanswer(ind) = sum(n==posAns(ind));
end
if sum(Uanswer == [1 1 1 0 1])==5
disp('correct answer')
else
disp('wrong')
end
%or other method
if strmatch(n,'abce')
disp('correct answer')
else
disp('wrong')
end
enoch adotey
enoch adotey 2015년 7월 2일
joseph am grateful you took your time to help me with the exact solution and it has given me a clearer understanding into programming. 2. i still need a clarification to simulate this: if assuming 1000 student took the exam with 30 questions, what will be the number of lucky_students that will have atleast 4 questions correct???

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Marine and Underwater Vehicles에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by