Question on using a while loop?

I have attached my script. I need to Add in code to keep track of the user’s total amount of money (balance), and Display the current balance to the user during each round of play. And At the end of each round, the user should be asked if he/she wants to play again. The games should only continue as long as the user wants to play again and the user still has money left. Im confused on how to keep track of the total balance throughout the game and getting the game to continue on. Would I use a menu statement inside a while loop?

 Respuesta aceptada

Walter Roberson
Walter Roberson el 24 de Oct. de 2015
wants_to_continue = true;
while wants_to_continue
...
your existing code
user_choice = input('Do you want to continue? (y/N)', 's');
wants_to_continue = strcmpi(user_choice, 'Y');
end

2 comentarios

Tyler Silva
Tyler Silva el 24 de Oct. de 2015
Is all my code, including my switch statements going to go into the while loop.
Walter Roberson
Walter Roberson el 24 de Oct. de 2015
Yes.

Iniciar sesión para comentar.

Más respuestas (1)

Ken Atwell
Ken Atwell el 24 de Oct. de 2015
To add the bet from the total, use:
Bank = Bank + Bet;
Subtraction will be similar, using "-" of course.
To get your program to loop, you could pop up a menu asking if they want to play again. Your while statement would look something like:
while Bank > 0 && play_again
...
end
Where "play_again" is a variable you create to capture the result of the next menu.

3 comentarios

Tyler Silva
Tyler Silva el 24 de Oct. de 2015
What exactly do i need to define in my while loop. Im still a little confused here. Thanks for the help though.
Ken Atwell
Ken Atwell el 24 de Oct. de 2015
Any code that need to run again and again needs to be inside the while loop.
Tyler Silva
Tyler Silva el 24 de Oct. de 2015
Editada: Tyler Silva el 24 de Oct. de 2015
could you please take a look at this, and see if I got it? Thanks!

Iniciar sesión para comentar.

Categorías

Más información sobre Just for fun en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 24 de Oct. de 2015

Comentada:

el 24 de Oct. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by