Filter löschen
Filter löschen

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

21 Ansichten (letzte 30 Tage)
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 Kommentare
Steven Lord
Steven Lord vor etwa 21 Stunden
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Milan Bansal
Milan Bansal vor etwa 21 Stunden
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!

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by