Info
此问题已关闭。 请重新打开它进行编辑或回答。
Sort them in the correct order?
3 次查看(过去 30 天)
显示 更早的评论
% if a>b we swap the roles of a and b.
if (a > b)
c = a; a = b; b = c;
end
function computerGuess (a, b)
% function computerGuess (a, b)
% Game : the computer must guess the integer
% in the interval (a,b) which the user is thinking of.
% the computer asks the user to think of a number
disp(['Think of a number in ' num2str(a) ...
' and ' num2str(b)]);
% We keep track of the previous guess. It is initialized as a-1:
old_guess = a-1;
% Takes the integer part of a and b
a = floor(a);
b = ceil (b);
% loops while the computer guesses
% this loop is exited with a break
while (true)
% as a guess, the computer gives the middle of [a,b]
guess = ceil( (a+b)/2 );
if ( guess == old_guess )
% if the old guess is the same, then the computer is nearly there, the answer is:
guess = guess - 1;
end
elseif ( answer == 2 )
% we must keep the lower interval
b = guess;
answer = menu(['The number is ' num2str(guess) ' ? '], ...
'correct!', too high!', too low!');
else
% we must keep the upper interval
a = guess;
end
if ( answer == 1)
disp('I win !');
break;
old_guess = guess;
end
end
3 个评论
Stephen23
2017-7-4
@Meenakshi S: do you want to ask us a question, or are you just showing off your code?
回答(0 个)
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!