The OR statement "|" is not working

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

답변 (1개)

KSSV
KSSV 2022년 3월 3일

1 개 추천

You may consider replacing the line
roll ~= 2 | 3 | 7 | 11 | 12
with
~ismember(roll,[2 3 7 11 12])

댓글 수: 3

William Day
William Day 2022년 3월 3일
편집: William Day 2022년 3월 3일
Doing that creates a pause at
while roll ~= point
KSSV
KSSV 2022년 3월 3일
MAy be it is not a pause......the while loop has gone infinite.. ?
William Day
William Day 2022년 3월 3일
편집: William Day 2022년 3월 3일
When I try to step through it it makes a new ismember page with things like:
if nargin == 3 && isstring(flag1)
flag1 = convertFlag(flag1);
if nargout < 2
lia = ismember(A, B, flag1);
else
[lia, locb] = ismember(A, B, flag1);
end
return
end
if nargin == 4 && (isstring(flag1) || isstring(flag2))
if isstring(flag1)
flag1 = convertFlag(flag1);
end
if isstring(flag2)
flag2 = convertFlag(flag2);
end
if nargout < 2
lia = ismember(A, B, flag1, flag2);
else
[lia, locb] = ismember(A, B, flag1, flag2);
end
return
end
And there are hundreds of lines
Perhaps because ismember is used for arrays instead of scaning for integers?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2022년 3월 3일

편집:

2022년 3월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by