% generate a random value for x
x = randi([1 100]);
% ask questions and ask for input
prompt1= 'x+4';
ans1 = inputdlg(sprintf("when x = %i \nWhat is x+4?",x));
myicon = imread("Jellybean.png");
%creates a while loop in the event of a correct answer
while input == x+4
msgbox('Correct! The frogs kiss your feet and you move on!',myicon);
%creates an if loop in the event of an incorrect answer
if input ~= x+4
msgbox('Incorrect! You are attacked by frogs! Try again to gain their trust.',myicon);
%retries the loop until the correct answer is given
continue
end
end
%i keep getting this error: Error using input Not enough input arguments. But im not sure where theres more than 1 input

 Akzeptierte Antwort

Star Strider
Star Strider am 30 Apr. 2022

0 Stimmen

I suspect the problem is the ‘input’ variable, since input is a function.
The solution is to rename ‘input’ to something else that makes sense in the context of the rest of the code.
Go frogs!

8 Kommentare

Isobel Leikis
Isobel Leikis am 30 Apr. 2022
yay frogs!
thank you for your help, i have changed the variable name.
x = randi([1 100]);
% asks questions and waits for user input
prompt1= 'x+4';
ans1 = inputdlg(sprintf("when x = %i \nWhat is x+4?",x));
myicon = imread("Jellybean.png");
% creates a while loop to handle the user input
while ans1 == x+4
msgbox(['Correct!';'The frogs kiss your feet and you move on!'],myicon);
% creates an if loop for the event of an incorrect answer
if ans1 ~= x+4
msgbox(['Incorrect!';'You are attacked by frogs! Try again to gain their trust.'],myicon);
% runs the loop until the correct answer is given
continue
end
end
Now i have this error: Operator '==' is not supported for operands of type 'cell'.
do i need to rethink my loops?
thank you!
Star Strider
Star Strider am 30 Apr. 2022
The loops are likely fine (although I did not run the complete code).
Similar problem — ‘ans1’ is a cell array (and this time, also a character array), so:
ans1 = inputdlg(sprintf("when x = %i \nWhat is x+4?",x))
ans1 = str2double(ans1)
The str2double function removes it from the cell array and evaluates it as a double-precision scalar.
The rest of the tests on it should work.
.
Isobel Leikis
Isobel Leikis am 1 Mai 2022
%Thank you for your help, that moved my program along further
% generate a random integer value for x between 1 and 100
x = randi([1 100]);
% asks a question and waits for user input
prompt1= 'x+4';
ans1 = inputdlg(sprintf("when x = %i \nWhat is x+4?",x));
% removes ans1 from the cell array and evaluates it as a double precision scalar
ans1 = str2double(ans1);
% creates a while loop to handle the user input
while ans1 == x+4
msgbox(['Correct!';'The frogs kiss your feet and you move on!'],myicon);
% creates an if loop for the event of an incorrect answer
if ans1 ~= x+4
msgbox(['Incorrect!';'You are attacked by frogs! Try again to gain their trust.'],myicon);
% runs the loop until the correct answer is given
continue
end
end
% I now keep getting this error: Error using vertcat Dimensions of arrays being concatenated are not consistent. I'm assuming it is still an issue with one of my variables.
Star Strider
Star Strider am 1 Mai 2022
It’s not an issue with the variables. It’s using character arrays.
One option is to create them as string variables —
msgbox(["Correct!";"The frogs kiss your feet and you move on!"]);
msgbox(["Incorrect!";"You are attacked by frogs! Try again to gain their trust."]);
That worked when I tried it. (I omitted the icon reference since I don’t have it.)
I tried to use this .png file with a transparent background, however I couldn’t get it to work.
.
Isobel Leikis
Isobel Leikis am 1 Mai 2022
i like the frog!
how did you change the variables to strings?
Star Strider
Star Strider am 1 Mai 2022
Thank you!
I put double quotes areound them:
"string"
'character array'
The string function will also work.
If you want to use character arrays, the sprintf function makes it easier:
msgbox(sprintf('Correct!\nThe frogs kiss your feet and you move on!'));
I tested it — that also works.
.
Isobel Leikis
Isobel Leikis am 1 Mai 2022
thank you so much for your help!
Star Strider
Star Strider am 1 Mai 2022
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by