Where should I add error checking to my program?
이전 댓글 표시
I'm trying to create a program that simulates a dice roll. The instructions given are: Write a matlab funtion called hitTarget that will accept one scalar argument, the target sum of two random 6-sided dice rolls. The function generates and displays two random numbers between 1 and 6 (like dice rolls) until the sum of the two die equals the target argument. Keep track of how many rolls it takes.
Sample Run:
>> hitTarget(8)
5 2
6 3
2 2
4 3
3 5
Target met, it took 5 rolls.
My code so far:
function [] = hitTarget( target )
%if(target>=2 && target<=12 && isinteger(target))
count = 0;
dieTotal = 0;
while(dieTotal~=target)
count = count + 1;
dieOne = randi(6,1,1);
dieTwo = randi(6,1,1);
result = [dieOne dieTwo];
disp(result);
dieTotal = sum(result);
if(dieTotal==target)
disp(['Target met, it took ', num2str(count), ' rolls.']);
end
end
%end
end
I commented out the error checking statement I need because the program will not run at all if I leave it there. But I don't know where else to put it.
Thanks for any help!!!
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!