Need help finding mistakes in code
Informazioni
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
Mostra commenti meno recenti
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.
Risposte (1)
Guillaume
il 22 Nov 2016
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.
Questa domanda è chiusa.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!