How to write a function that returns the amount of steps that happened before running into a wall?

1 次查看(过去 30 天)
Im trying to write a function that lets my object take a certain number_ of _steps in its current direction tracking how many steps it took before it runs into a wall. Then I want the function to return the number of steps taken before hitting the wall. So far I have this (code below) but its not working and im not sure what i did wrong.
function walk(number_of_steps)
steps_made_successfully= number_of_steps;
if steps 0
step_made_successfully 1
return(step_made_successfully)
end

采纳的回答

Image Analyst
Image Analyst 2022-10-26
So many things wrong with this. number_of_steps should be returned from the function, and it is not (yet). The input should be the distance from the start to the wall, and the steps length (stride distance).
function number_of_steps = walk(distanceToWall, stepLength)
number_of_steps= 0; % Initialize
distanceSoFar = 0
while distanceSoFar < distanceToWall && number_of_steps < 999999 % 999999 is the fail safe to prevent infinite loop.
% Here is where you write some more code.....
end % of while
end % of function

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by