The OR statement "|" is not working
Show older comments
I'm trying to make the program skip that last segment if any of those numbers are the active roll value, but when the roll value is equal to one of them, it still goes through.
n = input('How many rolls: ');
count = 0;
lose = 0;
win = 0;
pointlose = 0;
pointwin = 0;
while n > count
roll = randi([2 12]);
count = count + 1;
if roll == 7
fprintf('Win\n')
win = win + 1;
count = count + 1;
elseif roll == 11
fprintf('Win\n')
win = win + 1;
count = count + 1;
elseif roll == 2
fprintf('Lose\n')
lose = lose + 1;
count = count + 1;
elseif roll == 3
fprintf('Lose\n')
lose = lose + 1;
count = count + 1;
elseif roll == 12
fprintf('Lose\n')
lose = lose + 1;
count = count + 1;
end
if roll ~= 2 | 3 | 7 | 11 | 12
point = roll;
while roll ~= point
roll = randi([2 12]);
if roll == point
fprintf('point win\n')
pointwin = pointwin + 1;
elseif roll == 7
fprintf('point loss\n')
break
end
end
else
end
end
Answers (1)
KSSV
on 3 Mar 2022
You may consider replacing the line
roll ~= 2 | 3 | 7 | 11 | 12
with
~ismember(roll,[2 3 7 11 12])
3 Comments
William Day
on 3 Mar 2022
Edited: William Day
on 3 Mar 2022
KSSV
on 3 Mar 2022
MAy be it is not a pause......the while loop has gone infinite.. ?
William Day
on 3 Mar 2022
Edited: William Day
on 3 Mar 2022
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!