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

조회 수: 2 (최근 30일)
Blain Wood
Blain Wood 2022년 4월 14일
댓글: Blain Wood 2022년 4월 14일
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