(1) guess higher and guess lower were in the wrong position;
(2) the count guessTimes should be initialized outside of the loop
(3) Every prompt will increase the guessTimes by 1.
y=randi(100);
guessTimes = 0;
while (1)
prompt= '\nguess a value between 1 and 100: ';
x=input(prompt);
guessTimes = guessTimes + 1;
disp(x)
if (x<y)
disp('guess lower');
elseif (x>y)
disp('guess higher');
else
disp('you win');
fprintf('guess times:%d\n',guessTimes);
break;
end
end