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 Center および File ExchangeLanguage Fundamentals についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by