Need help finding mistakes in code
信息
此问题已关闭。 请重新打开它进行编辑或回答。
显示 更早的评论
When I run the program I don't actually get any matlab errors but our instructor said there was mistakes within the code to actually get the program to give an appropriate answer.
So far I have found that line 35 should be "score_A = 0;" and line 53 should be "score_diff = abs(score_P-score_A);"
I need help finding all the mistakes.
0 个评论
回答(1 个)
Guillaume
2016-11-22
If just by reading the code, you can't see what is wrong with it, I suggest you use the debugger and step through the code line by line observing how the variables change and see if it conforms to your expectations. You'll quickly see where it goes wrong.
At first glance:
game_over = 2; % this will change to 1 once the game is over
while game_over == 0
%...
end
clearly the loop body will never be executed. Rather than using numerical values that have no meaning and are easy to confuse. (Was it 2, 1, or 0 for game_over?), use logical values that immediately make sense:
game_over = false; %since game is not over yet
while ~game_over %while the game is not over
%... do something
if ...
game_over = true;
end
end
2nd problem: you create a variable prob that you never use.
I've not looked any further. As I said, use the debugger.
0 个评论
此问题已关闭。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!