컴퓨터가 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();
}
}
무엇이 문제입니까? – Ben
ifs가 필요할 수도있는 곳이 있습니다. –
매번'while' 루프를 빠져 나올 때마다 여러 개의 프롬프트가 출력됩니다. 일단 match를 찾으면 while 루프에서 빠져 나올 수 있지만 내부 while 루프를 if 문으로 대체하는 것이 더 나을 것입니다. –