随机数猜测游戏

3
我正在制作一个随机数字猜测游戏,电脑会想一个介于1到100之间的数字。然后它会问你这个数字是多少,并告诉你你是否正确。然而,每当我调试时,出于某种原因,它会说它比实际的随机数高或低。此外,它同时显示了这两个陈述。另外,我不确定如何表达这个人已经猜了几次。这是我的不成功代码。
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();
    }
}

8
有些时候你可能需要用"if"代替"while"。 - Anthony Pegram
每次循环时,您都会穿过每个while循环,这就是为什么会打印多个提示的原因。一旦找到匹配项,您可以跳出while循环,但最好只是用if语句替换内部的while循环。 - Cody Gray
如果这是作业,请添加#homework标签。 - Muad'Dib
不确定这是否值得被踩,这只是一个非常初级的编程问题。我们都从某个地方开始。 - Mike Christensen
@AsharAslam - 根据您分享的要求,接受的解决方案仅包含两个if语句。由于这显然是一项作业,基于您之前在其他主题上的5个问题,我将标记它为这样的作业。 - Security Hound
显示剩余2条评论
8个回答

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.ReadLine());
    
                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 时才终止! - Olivier Jacot-Descombes
2
如果你第一次猜对了,它会再次问你“你能猜出它是什么吗?” - Phil
谢谢Phil,我已经更正了代码,只有在答案不正确的情况下才会输出。 - OnResolve

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
尝试将while改为if,例如:
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("I am thinking of a number between 1-100.  Can you guess what it is?");

while循环内部设置提示,这样它会在每个提示之前继续询问您。


0

你需要将你的While循环改为if-then-else语句

当条件为真时,while循环会一直运行其代码。 因此,在你的代码中,你运行第一个循环 - 基本上是永远的,因为你没有重置条件中的任何一个值。

如果你的while循环退出,那么其他while循环也会出现同样的问题。 你需要像这样:

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

0

正如其他人所说,您在错误地使用while,而实际上应该使用if

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

你好,对于你来说可能没问题,但是对于其他想尝试的人来说:

using System;

namespace Exemple
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            int returnvalue = random.Next(1, 51);

            Console.WriteLine(" Guess a number between 1 to 51 ");
            int response = Convert.ToInt32(Console.ReadLine());

            while (response > returnvalue)
            {
                Console.WriteLine($"No the number is low than {response} try again !");
                response = Convert.ToInt32(Console.ReadLine());
            }

            while (response < returnvalue)
            {
                Console.WriteLine($"No the number is high than {response} try again !");
                response = Convert.ToInt32(Console.ReadLine());
            }

            while (response != returnvalue)
            {
                Console.WriteLine($" wrong answer {response} is not the good response try again !");
                response = Convert.ToInt32(Console.ReadLine());
            }

            Console.WriteLine($"Good ! Its  {returnvalue}");

        }
    }
}

0

生成一个1到9之间(包括1和9)的随机数。要求用户猜测这个数字,然后告诉他们他们猜得太低、太高还是正好。额外功能:保持游戏进行直到用户输入“退出”,跟踪用户猜了多少次,并在游戏结束时打印出来。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接