Creating a function but not getting all the Outputs desired...
Show older comments
What am I doing wrong I need it to display results [die1, die2, resultstr] It is only displaying the first die value
________________________________________________________________________________
function [die1, die2, resultstr]=rolldice
% function [dice1,dice2,resultstr]=rolldice;
% simulates roll of two fair dice% returns
% die1 an integer in the range [1,6]
% die2 an integer in the range [1,6]
% resultstr string with result (sumd=die1+die2)
% 'snake eyes' sumd=2
% 'ace-deuce' sumd=3
% 'yo' sumd=11
% 'boxcars' sumd=12
% 'natural' sumd=7
% 'hard six' 3 3
% 'hard four' 2 2
_________________________________________________
die1=randi([1,6])
die2=randi([1,6])
sumd=die1+die2
if sumd==4||sumd==5||sumd==6||sumd==8||sumd==9||sumd==10
resultstr=(' ')
elseif sumd==2
resultstr=('snake eyes')
elseif sumd==3
resultstr=('ace-deuce')
elseif sumd==11
resultstr=('yo')
elseif sumd==12
resultstr=('boxcars')
elseif sumd==7
resultstr=('natural')
elseif (die1==3)&&(die2==3)
resultstr=('hard six')
elseif (die1==2)&&(die2==2)
resultstr=('hard four')
end
end
Answers (1)
Walter Roberson
on 9 Nov 2016
You need to use three outputs when you call the function, as otherwise the default is to display only one.
[die1, die2, resultstr]=rolldice
should display all the outputs
Categories
Find more on Clustering 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!