MATLAB Answers


Guess number game and hint

Takashi Fukushima さんによって質問されました 2019 年 11 月 2 日
最新アクティビティ Takashi Fukushima さんによって コメントされました 2019 年 11 月 2 日
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.
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);
I appreciate your reply in advance.

  0 件のコメント

サインイン to comment.

1 件の回答

回答者: darova
2019 年 11 月 2 日

Use abs function
if abs(answer-guess) <= 5
disp('you are close!')

  1 件のコメント

Takashi Fukushima 2019 年 11 月 2 日
Thank you so much! That is what I have benn missing!

サインイン to comment.

Translated by