Have a partial code but I cant get it to perform where if falls to the right.

2 visualizaciones (últimos 30 días)
clc; clear;
stepx=0;
stepy=0;
lf=0;
rf=0;
docked=0;
trials=10^5;
for i=1:trials
stepx=0;
stepy=0;
% Looking to keep it between the stepx parameters and less that the
% stepy parameter before exiting while loop
while stepx>-9 && stepx<9 || stepy<75
chance=randi(100);
if chance<=75
stepy=stepy+1;
elseif 75>chance && chance<=89
stepx=stepx+1;
elseif 89>chance
stepx=stepx-1;
end
end
% adding up the left fall, right fall, and docked
if stepx==-9
lf=lf+1;
elseif stepx==9
rf=rf+1;
elseif stepy == 75
docked = docked+1;
end
end
disp(['The pirate fell off to the left ',num2str(lf),' times.'])
disp(['This was ',num2str(lf/(trials)*100),'% of the time.'])
disp(' ')
disp(['The pirate fell off to the right ',num2str(rf),' times.'])
disp(['This was ',num2str(rf/(trials)*100),'% of the time.'])
disp(' ')
disp(['The pirate got on board ',num2str(docked),' times.'])
disp(['This was ',num2str(docked/(trials)*100),'% of the time.'])

Respuestas (1)

Geoff Hayes
Geoff Hayes el 14 de Abr. de 2022
Editada: Geoff Hayes el 14 de Abr. de 2022
@Blain Wood - I think you need to change the first condition of
elseif 75>chance && chance<=89
to
elseif 75 < chance && chance <= 89
Also, change
elseif 89>chance
to
elseif 89 < chance
or just use an else instead.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by