Need help making my riddles work

1 Ansicht (letzte 30 Tage)
Breanna Sokolik
Breanna Sokolik am 24 Okt. 2020
Beantwortet: Rik am 24 Okt. 2020
%Asking the user 3 riddles.
riddle1prompt='The person who built it sold it. The person who bought it never used it. The person who used it never saw it. What is it? ';
riddle1=input(riddle1prompt,'s');
riddle1='Coffin'
fprintf('\n')
if (riddle1==Coffin)
%Only asks next riddle if the user got the first one right.
riddle2prompt='Nice job, sadly Jack and Ethan did not fair so well. Oh well. Your next riddle is; I dont have eyes, but once I did see. Once I had thoughts, but now Im white and empty. What am I? ';
riddle2=input(riddle2prompt,'s');
riddle2='Skull'
elseif(riddle1~=Coffin)
%The game is over and the user has to start over.
fprintf('Oh no, what did I say about not messing up. ITS GAME OVER FOR YOU,%s.',name)
firstroom=false;
end
I feel like i am very close to the riddle working, the elseifs do not work and the riddle 1 input is being lost. What am i doing wrong here?
  1 Kommentar
Rik
Rik am 24 Okt. 2020
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Rik
Rik am 24 Okt. 2020
You should use the debugger to go through your code line by line and see what is happening with your variables.
You are overwriting the answer from the user with the solution. Then you use == instead of strcmp, and you never defined the variable Coffin. Also, the only way your code reaches your elseif is if the two arrays don't match. So the only way that condition will be false is if there is a cosmic ray that changes a bit in your memory in the microseconds between the evaluation of the two conditions. (so you can replace that elseif with else)

Kategorien

Mehr zu Board games finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by