Help with conidtional loops?

1 回表示 (過去 30 日間)
Taylor Gates
Taylor Gates 2018 年 7 月 19 日
編集済み: OCDER 2018 年 7 月 19 日
Everything in the code works like I want it to except I need the code to end if a guess is higher than 10. it will display 'dumb' but also needs to end the program. I can't get it to break the code.
%%Guess Number Game
R=floor ( rand()*10 );
count=0;
for i=0:5
while(i~=5)
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')
count=0;
break;
end
end
end
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 7 月 19 日
Why are you using while inside of for? The body of your while never modifies i, so the while loop is not going to end until it hits one of the two "break" statements.
Your current code would play the game 5 times, once for i = 0, once for i = 1, 2, 3, 4. Then your "for" loop extends to i = 5, but your while excludes that case so nothing would be done for that case, so why bother to have the while, why not just end i at 4 ?
Taylor Gates
Taylor Gates 2018 年 7 月 19 日
編集済み: Walter Roberson 2018 年 7 月 19 日
Here's what I just came up with. It will end it if a guess is higher than 10 and display dumb. However, it will end the game after 3 wrong guesses. After 3 guesses it should say you failed the game but let you try again lets say 4 more times and THEN end the whole code. Any insight?
%%Guess Number Game
R=floor ( rand()*10 );
count=0;
i=0
while(i<6)
guess=input('Guess a number between 0 and 10')
if (guess>10)
i=5
disp('DUMB')
end
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
count=count+1
if count==3;
disp ('You Failed The Game, Try Again')
count=0;
break;
end
i=i+1
end

サインインしてコメントする。

採用された回答

Christopher Wallace
Christopher Wallace 2018 年 7 月 19 日
use 'return' instead of 'break'

その他の回答 (1 件)

OCDER
OCDER 2018 年 7 月 19 日
編集済み: OCDER 2018 年 7 月 19 日
Keep track of how many games and how many trials were done, and the status of the game as WonGame = 0 or WonGame = 1.
%%Guess Number Game
WonGame = 0;
GameCount = 0;
while ~WonGame
if GameCount >= 5
disp('You Failed The Game 5 Times! No more!')
break
end
R = randi(9); %floor( rand()*10 );
Guess = Inf;
count = 0;
while 1
%guess = round(input('Guess a number between 0 and 10: ')); %DID YOU WANT IT 0,..., 10? or 1,...,9???
guess = round(input('Guess an integer between 0 and 10: '));
if guess>=10 || guess <= 0
disp('DUMB. Integer must be > 0 and < 10.')
elseif (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')
WonGame = 1;
break
end
count = count + 1;
if count == 3
disp('You Failed The Guess in 3 Tries')
disp(['Answer was: ' num2str(R)]);
break
end
end
GameCount = GameCount + 1;
end
  2 件のコメント
Taylor Gates
Taylor Gates 2018 年 7 月 19 日
Here's what I just came up with. It will end it if a guess is higher than 10 and display dumb. However, it will end the game after 3 wrong guesses. After 3 guesses it should say you failed the game but let you try again lets say 4 more times and THEN end the whole code. Any insight?
%%Guess Number Game
R=floor ( rand()*10 ); count=0;
i=0 while(i<6)
guess=input('Guess a number between 0 and 10')
if (guess>10)
i=5
disp('DUMB')
end
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
count=count+1
if count==3;
disp ('You Failed The Game, Try Again')
count=0;
break;
end
i=i+1
end
OCDER
OCDER 2018 年 7 月 19 日
See the edited answer above.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by