returning to the start of the game

조회 수: 5 (최근 30일)
James Webber
James Webber 2022년 1월 6일
i am writting the code for battleships and there is a section that if yuo answer no then it should take you back to the beginging however this does not work for me and it finishes the progam.
the whole code is withing a while loop which works, but for some reasson when i hit no it does not go back to the start.
this is what it looks like
s=input ('would you like to see the game stastics 1,yes 2,no:')
switch s
case 1
num_player = 20;
WLT = zeros(num_player,3);
%disp stastics
if have_a_winner
WLT(winner_index,1) = WLT(winner_index,1) + 1;
WLT(loser_index,2) = WLT(loser_index,2) + 1;
else
WLT(player1_index,3) = WLT(player1_index,3) + 1;
WLT(player2_index,3) = WLT(player2_index,3) + 1;
end
case 2
disp('main menu')
playOn = false;
break
end
case 1 works however case 2 chould go back to the beginging but it does not, any ideas on how to fix this
  댓글 수: 1
Voss
Voss 2022년 1월 6일
What if you change break to continue?

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

답변 (1개)

Harikrishnan Balachandran Nair
Harikrishnan Balachandran Nair 2022년 1월 24일
Hi,
It is my understanding that you have a switch case inside a while loop , and the loop is being terminated on executing the 'break' statement.
This is the expected behaviour as the 'break' statement exits the for/while loop completely .As suggested in the comments, changing the 'break' statement to ' continue' might resolve the issue that you are facing, as 'continue' passes control to the next iteration. Hope this helps!

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by