フィルターのクリア

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 件のコメント
omer
omer 約21時間 前
yes i know
i just dont know how to write that
Steven Lord
Steven Lord 約21時間 前
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 約21時間 前
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 Center および File ExchangeLoops 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