How to use the break in a loop?

조회 수: 153 (최근 30일)
Taylor Gates
Taylor Gates 2018년 7월 18일
댓글: Christopher Wallace 2018년 7월 19일
Im trying to create a code that modifies a game. This game is you have 3 tries to guess a number 0-10. If you fail 10 times it will end the game and display 'dumb'. I have done all of that up to the point where it displays dumb, but it will not end the game it just continues. What do I need to change for it to end?
R=floor ( rand()*10 );
count=0;
while (1)
for i=1:10
guess=input('Guess a number between 0 to 10 ');
if (R>guess)
disp('Your guess is too small')
elseif (R<guess)
disp('Your guess is too large')
elseif (R==guess)
disp('You are correct! It is ');guess
break;
end
count=count+1
if(count==3)
disp('You Failed The Game, Try Again')
count=0;
if i==10
disp('DUMB')
break;
end
end
end
end
  댓글 수: 1
Aquatris
Aquatris 2018년 7월 18일
The break you have breaks the first loop it is in, which is the for loop in your code. I do not think you need the while loop since after 10 tries you want to end the game. Remove the while loop and if statement with the break and it should work as you intended to.

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

채택된 답변

Christopher Wallace
Christopher Wallace 2018년 7월 18일
Hi Taylor,
The problem with the 'break' statement is that you have 2 loops executing, a while loop and a for loop.
When the break statement is executed it exits the for loop but starts back up since the while loop is still executing.
You could add a similar 'if' statement outside the for loop but still inside the while statement to break again.
end % end of for loop
if i==30
break
end
end % end of while loop
There is another issue in your code since it only allows 10 guesses before restarting and since each game is 3 guesses long it end up restarting after 1 guess in the 4th game.
Best of Luck,
Chris
  댓글 수: 2
Taylor Gates
Taylor Gates 2018년 7월 18일
Okay so can you explain that for this modified version of that code? Same principle, 3 guesses or fail but only allowed to fail 10 times. I have it where it breaks at 3 guesses but can you explain where and what the loop and break would go in this code?
R=floor ( rand()*10 ); count=0;
for i=0:10 while(i==10)
guess=input('Guess a number between 0 and 10')
if (R>guess)
disp('Your Guess Is Too Small')
elseif (R<guess)
disp('Your Guess Is Too Large')
elseif (R==guess)
disp('You are Correct! It is')
end
if (guess>10)
disp('DUMB')
break;
end
count=count+1
if count==3;
disp ('You Failed The Game, Try Again')
break;
end
end
end
Christopher Wallace
Christopher Wallace 2018년 7월 19일
Taylor,
Is there a requirement that you have to use the 'break' command? If you have multiple loops but need to end a function early use 'return'.

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

추가 답변 (1개)

KSSV
KSSV 2018년 7월 18일
N = 10 ; % This number to be guessed
Fail = 0 ;
for i = 1:10
S = input('Guess the Number:','s') ;
if N==str2num(S)
disp('You won') ;
break
else
Fail = Fail+1 ;
end
if Fail == 3
disp('You are dumb')
break
end
end

카테고리

Help CenterFile Exchange에서 Video games에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by