Question on using a while loop?

조회 수: 5 (최근 30일)
Tyler Silva
Tyler Silva 2015년 10월 24일
댓글: Walter Roberson 2015년 10월 24일
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?

채택된 답변

Walter Roberson
Walter Roberson 2015년 10월 24일
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
Tyler Silva
Tyler Silva 2015년 10월 24일
Is all my code, including my switch statements going to go into the while loop.
Walter Roberson
Walter Roberson 2015년 10월 24일
Yes.

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

추가 답변 (1개)

Ken Atwell
Ken Atwell 2015년 10월 24일
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
Ken Atwell
Ken Atwell 2015년 10월 24일
Any code that need to run again and again needs to be inside the while loop.
Tyler Silva
Tyler Silva 2015년 10월 24일
편집: Tyler Silva 2015년 10월 24일
could you please take a look at this, and see if I got it? Thanks!

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

카테고리

Help CenterFile Exchange에서 Conway's Game of Life에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by