2016-09-05 2 views
0

나는 1-100 사이의 숫자 게임을 추측했다. 이는 그들이 가까이있어라는 메시지와 함께 인사, 사용자 수에 근접하는 경우, 5 개 개의 번호 내 말도록C# - 코드를 추가해야합니다! 숫자를 추측

class Program 
{ 
    static void Main(string[] args) 
    { 
     while (true) 
     { 
      int randno = Newnum(1, 101); 
      int count = 1; 
      while (true) 
      { 
       Console.Write("Guess a number between 1 and 100, or press 0 to quit: "); 
       int input = Convert.ToInt32(Console.ReadLine()); 
       if (input == 0) 
        return; 
       else if (input < randno) 
       { 
        Console.WriteLine("Unlucky, that number is too low - have another go!"); 
        ++count; 
        continue; 
       } 
       else if (input > randno) 
       { 
        Console.WriteLine("Unlucky, that number is too high - have another go!"); 
        ++count; 
        continue; 
       } 
       else 
       { 
        Console.WriteLine("Well done - you guessed it! The number was {0}.", randno); 
        Console.WriteLine("It took you {0} {1}.\n", count, count == 1 ? "attempt" : "attempts to guess it right"); 
        break; 
       } 
      } 
     } 

    } 
    static int Newnum(int min, int max) 
    { 
     Random random = new Random(); 
     return random.Next(min, max); 
    } 
} 

어떻게 편집 할 수 있습니다 .. 내 코드?

+0

당신은 정말 묻는 "숫자 X는 숫자 Y의 5 이내 인 경우 어떻게 알 수"? 그렇다면 지금까지 시도한 것을 보여줌으로써 질문을 줄이는 것이 좋습니다. –

답변

7

당신은 Math.Abs 사용할 수 있습니다

int diff = Math.Abs(input - randno); 
if(diff <= 5) 
{ 
    // say him that he's close 
} 
관련 문제