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

2 次查看(过去 30 天)
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.'])

回答(1 个)

Geoff Hayes
Geoff Hayes 2022-4-14
编辑:Geoff Hayes 2022-4-14
@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.

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by