Guess number game and hint

1 visualización (últimos 30 días)
Takashi Fukushima
Takashi Fukushima el 2 de Nov. de 2019
Comentada: Takashi Fukushima el 2 de Nov. de 2019
Hello,
I would like to write a program for number guessing game, but I want to add one feature which is that if the guess is close to the answer +-5, I want the program to say "Your answer is close" after the disp "Your guess is too much" or "small".
Here is what I have now.
Answer=50
clc
Guess1=input("Enter your guess: ");
if Guess1==Answer
disp("You got it: " + Guess1);
elseif Guess1>Answer
disp("Too much: " + Guess1);
elseif Guess1<Answer
disp("Too small: " + Guess1);
end
I appreciate your reply in advance.
Thanks,

Respuesta aceptada

darova
darova el 2 de Nov. de 2019
Use abs function
if abs(answer-guess) <= 5
disp('you are close!')
end
  1 comentario
Takashi Fukushima
Takashi Fukushima el 2 de Nov. de 2019
Thank you so much! That is what I have benn missing!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Number games en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by