필터 지우기
필터 지우기

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에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by