필터 지우기
필터 지우기

Guess number game and hint

조회 수: 1 (최근 30일)
Takashi Fukushima
Takashi Fukushima 2019년 11월 2일
댓글: Takashi Fukushima 2019년 11월 2일
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,

채택된 답변

darova
darova 2019년 11월 2일
Use abs function
if abs(answer-guess) <= 5
disp('you are close!')
end
  댓글 수: 1
Takashi Fukushima
Takashi Fukushima 2019년 11월 2일
Thank you so much! That is what I have benn missing!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Number games에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by