stugeling with if statement displaying results

2 次查看(过去 30 天)
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 中查找有关 Just for fun 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by