stugeling with if statement displaying results

조회 수: 3 (최근 30일)
Jacob
Jacob 2013년 8월 8일
I am trying to write a file to simulate a roulette game, I am up to the if statement and trying to get matlab to display either green,red,or black for a random spin of the roulette table. it will display green as i used an if statement for that but it wont display red or black...i think it has something to do with the way i entered the numbers... this is how i have set out the file....
%% Roulette simulation clear, clc x = randi([0,36]) if x==0 disp('green') elseif x==[1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36] disp('red') elseif x==[2,4,6,8,10,11,13,15,17,20,22,24,26,28,29,31,33,35] disp('black') end

답변 (1개)

kjetil87
kjetil87 2013년 8월 8일
You are correct. It is how you compare x. The result will be a vector containing lots of false and maybe one true. If you change it to ;
elseif any(x==[1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36])
It will work.

카테고리

Help CenterFile Exchange에서 Word games에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by