필터 지우기
필터 지우기

how to write a code on matlab for a function that runs until she gets a regional answer

조회 수: 21 (최근 30일)
hi everyone!
i have a code i need to write-
i need to write a function that gets from the user amount of money he has and ask him how much money he wants to bet on.
the function needs to check that the value is regional and make sense and afterwards, if the value is good it will stop running, is isnt it will keep running until the user will insert regional value.
so far i wrote this- and its working but i dont know how to make the function keep running till the value is good
could use your help it will be helpful
thanx
function betamount = bet(money)
betamount= input('on what amount do you want to bet on?: ');
if ~isnumeric(betamount)|| betamount<=0 || betamount>money
disp('the betamount is invaild, try again');
end
end
  댓글 수: 3
Steven Lord
Steven Lord 대략 20시간 전
Why don't you show us how you've tried to write that using the while keyword. Seeing what you tried will help us tailor our suggestions for how to correct your code. It may be that you've almost got it right and we just need to explain one little detail that will make the code work and improve your understanding of how while (and/or MATLAB in general) works for the future.

댓글을 달려면 로그인하십시오.

답변 (1개)

Milan Bansal
Milan Bansal 대략 20시간 전
Hi omer
To make the function keep asking for a valid value for "betamount" until a valid value for "betamount is entered", you can use a while loop along with continue and break statements.
Here is how you can modify your code:
function betamount = bet(money)
while(1)
betamount= input('on what amount do you want to bet on?: ');
if ~isnumeric(betamount)|| betamount<=0 || betamount>money
disp('the betamount is invaild, try again');
else
break;
end
end
end
Please refer to the documention link to learn more:
Hope this helps!

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by