2012-01-31 3 views
3

컴퓨터가 1 ~ 100의 숫자로 생각하는 난수 게임을 만들고 있습니다. 그런 다음 그것이 무엇인지 묻고 당신이 옳았는지 또는 틀린지를 알려줍니다. 그러나 디버깅 할 때마다 어떤 이유로 실제 난수보다 높거나 낮다고 표시됩니다. 게다가, 그것은 그 진술 중 2 개를 즉시 말합니다. 또한 나는 얼마나 많은 추측을했는지 말할 방법이 확실치 않습니다. 여기 내 실패한 코드입니다.임의 수 추측 게임

static void Main(string[] args) 
{ 
    Random random = new Random(); 

    int returnValue = random.Next(1, 100); 
    int Guess = 0; 

    Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?"); 

    while (Guess != returnValue) 
    { 
     Guess = Convert.ToInt32(Console.Read()); 

     while (Guess < returnValue) 
     { 
      Console.WriteLine("No, the number I am thinking of is higher than " + Guess + " . Can you guess what it is?"); 
      Console.ReadLine(); 

     } 
     while (Guess > returnValue) 
     { 
      Console.WriteLine("No, the number I am thinking of is lower than " + Guess + " . Can you guess what it is"); 
      Console.ReadLine(); 
     } 
    } 
    while (Guess == returnValue) 
    { 
     Console.WriteLine("Well done! The answer was " + returnValue); 
     Console.ReadLine(); 
    } 
} 
+0

무엇이 문제입니까? – Ben

+7

ifs가 필요할 수도있는 곳이 있습니다. –

+0

매번'while' 루프를 빠져 나올 때마다 여러 개의 프롬프트가 출력됩니다. 일단 match를 찾으면 while 루프에서 빠져 나올 수 있지만 내부 while 루프를 if 문으로 대체하는 것이 더 나을 것입니다. –

답변

5

. while 문은 IF 문과 마찬가지로 부울 조건을 취합니다.

static void Main(string[] args) 

{ 

Random random = new Random(); 

int returnValue = random.Next(1, 100); 

     int Guess = 0; 

     Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?"); 

     while (Guess != returnValue) 
     { 
      Guess = Convert.ToInt32(Console.Read()); 

      if (Guess < returnValue) 
      { 
       Console.WriteLine("No, the number I am thinking of is higher than " + Guess + ". Can you guess what it is?"); 
      } 
      else if (Guess > returnValue) 
      { 
       Console.WriteLine("No, the number I am thinking of is lower than " + Guess + ". Can you guess what it is?"); 
      } 

     } 

     Console.WriteLine("Well done! The answer was " + returnValue); 
     Console.ReadLine(); 

} 
+1

'if'는'Guess == returnValue' 일 때만 끝나기 때문에 더 이상 사용되지 않습니다! –

+2

처음으로 정확하게 추측하면 "그것이 무엇인지 추측 할 수 있습니까?"라고 묻습니다. 다시. – Phil

+0

감사합니다 Phil, 답변이 정확하지 않은 경우에만 출력하도록 수정했습니다. – OnResolve

0

대신 whileif의을보십시오. 예를 들면 :

if (Guess < returnValue) 
{ 
    Console.WriteLine("No, the number I am thinking of is higher than " + Guess + " . Can you guess what it is?"); 
} 
if (Guess > returnValue) 
{ 
    Console.WriteLine("No, the number I am thinking of is lower than " + Guess + " . Can you guess what it is"); 
} 

또한 넣어 할 수 있습니다 다음 while 루프 내부

Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?"); 

프롬프트가 각 프롬프트 전에를 묻는 유지할 수 있도록.

0

당신은 잠시 한 문장이 참으로 해당 코드를 실행하는 경우 - 당시 다른 문

While 루프를 변경해야합니다. 그래서 코드에서 처음 조건을 실행합니다. 기본적으로 조건의 값 중 하나를 재설정하지 않기 때문에 영원히 작동합니다.

while 루프가 종료되면 다른 while 루프와 동일한 문제가 발생합니다. 이 뭔가를 원하는 : 당신은 불필요한 반복의 많은을 사용하고

if (guess > myValue) { // do something } 
else (guess < myValue) {//do something else} 
else { // do a third thing } 
1

원하는대로 논리를 재구성하여보십시오. 다른 사람이 말했듯이

Random r = new Random(); 

int val = r.Next(1, 100); 
int guess = 0; 
bool correct = false; 

Console.WriteLine("I'm thinking of a number between 1 and 100."); 

while (!correct) 
{ 
    Console.Write("Guess: "); 
    string input = Console.ReadLine(); 

    if (!int.TryParse(input, out guess)) 
    { 
     Console.WriteLine("That's not a number."); 
     continue; 
    } 

    if (guess < val) 
    { 
     Console.WriteLine("No, the number I'm thinking is higher than that number."); 
    } 
    else if (guess > val) 
    { 
     Console.WriteLine("No, the number I'm thinking is lower than that number."); 
    } 
    else 
    { 
     correct = true; 
     Console.WriteLine("You guessed right!"); 
    } 
} 
0

, 당신은 if 정말 neeeded됩니다 while을 오용하고 있습니다.

static void Main(string[] args) 
{ 

    Random random = new Random(); 

    int returnValue = random.Next(1, 100); 
    int Guess = 0; 
    int numGuesses = 0; 

    Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?"); 

    while (Guess != returnValue) 
    { 
     Guess = Convert.ToInt32(Console.Read()); 
     string line = Console.ReadLine(); // Get string from user 
     if (!int.TryParse(line, out Guess)) // Try to parse the string as an integer 
      Console.WriteLine("Not an integer!"); 
     else { 
      numGuesses++; 
      if (Guess < returnValue) 
      { 
       Console.WriteLine("No, the number I am thinking of is higher than " + Guess + " . Can you guess what it is?"); 
      } 
      if (Guess > returnValue) 
      { 
       Console.WriteLine("No, the number I am thinking of is lower than " + Guess + " . Can you guess what it is"); 
      } 
     } 
    } 
    Console.WriteLine("Well done! The answer was " + returnValue + ".\nYou took " + numGuesses + " guesses."); 
} 
0

야 ...

int total = 1, 
      low = 0, 
      high = 0; 
     int ranNum1, 
      guess; 

     string guessStr; 

     Random ranNumGen = new Random(); 
     ranNum1 = ranNumGen.Next(1, 10); 

     Console.Write("Enter your guess >> "); 
     guessStr = Console.ReadLine(); 
     guess = Convert.ToInt16(guessStr); 

     while (guess != ranNum1) 
     { 
      while (guess < ranNum1) 
      { 
       Console.WriteLine("Your guess is to low, try again."); 
       Console.Write("\nEnter your guess >> "); 
       guessStr = Console.ReadLine(); 
       guess = Convert.ToInt16(guessStr); 
       ++total; 
       ++low; 
      } 
      while (guess > ranNum1) 
      { 
       Console.WriteLine("Your guess is to high, try again."); 
       Console.Write("\nEnter your guess >> "); 
       guessStr = Console.ReadLine(); 
       guess = Convert.ToInt16(guessStr); 
       ++total; 
       ++high; 
      } 
     } 
     //total = low + high; 
     Console.WriteLine("It took you {0} guesses to correctly guess {1}", total, ranNum1); 
0

(9 1을 포함하고) 1과 9 사이의 난수를 생성한다. 사용자에게 숫자를 알려달라고 요청한 다음 너무 낮거나 높거나 정확하게 맞는지 여부를 알려줍니다. 추가 정보 : 사용자 유형 '종료'가 사용자가 취한 추측 수를 추적 할 때까지 계속 게임을 진행하십시오. 게임이 끝나면이를 프린트하십시오.