随机猜数字游戏

发布于 2025-01-01 07:56:07 字数 1067 浏览 1 评论 0原文

我正在制作一个随机猜数游戏,计算机会想到 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();
    }
}

I am making a random number guessing game which the computer thinks of a number between 1-100. It then asks you what it is and tells you if you are right or wrong. However, whenever I debug, it says that it is higher or lower than the actual random number for some reason. Plus, it says two of those statements at once. Also, I'm not sure how to say how many guesses the person has taken. Here is my unsuccessful code.

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

凑诗 2025-01-08 07:56:07

您使用了很多不需要的迭代。 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();
            
    }

You're using a lot of unneeded iteration. The while statement takes a Boolean condition just like an IF statement.

    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();
            
    }
残疾 2025-01-08 07:56:07

尝试重构逻辑,使其完全符合您的要求。

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!");
    }
}

Try to restructure the logic so it does exactly what you want.

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!");
    }
}
柳絮泡泡 2025-01-08 07:56:07

尝试使用 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 循环中,这样它就会在每个提示符之前不断询问您。

Try making the whiles ifs instead. Such as:

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");
}

You also might want to put the:

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

prompt inside the while loop so it will keep asking you before each prompt.

呆头 2025-01-08 07:56:07

您需要将 While 循环更改为 if-then-else 语句,

只要该语句为 true,while 就会运行其代码。
因此,在您的代码中,您运行第一个 - 基本上永远运行,因为您没有重置条件中的任何一个值。

如果您的 while 循环要退出,那么其他 while 循环也会遇到同样的问题。
你想要这样的东西:

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

you need to change your While loops to if-then-else statements

a while will run its code as long as the statement is true.
so, in your code, you run the first one--basically forever because you are not resetting either of the values in your condition.

if your while loop WERE to exit, then you have the same problem with the other while loops.
you want something like this:

if ( guess > myValue ) { // do something }
else ( guess < myValue ) {//do something else}
else { // do a third thing }
笑忘罢 2025-01-08 07:56:07

正如其他人所说,您在真正需要 if 的地方滥用了 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.");
}

As others have said, you are misusing while where if is really neeeded.

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.");
}
谷夏 2025-01-08 07:56:07

老兄...

    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);

Dude...

    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);
偷得浮生 2025-01-08 07:56:07

生成 1 到 9 之间的随机数(包括 1 和 9)。要求用户得到这个数字,然后告诉他们是否猜得太低或太高,或者完全正确。附加功能:让游戏继续进行,直到用户输入“退出”,跟踪用户进行了多少次猜测,并在游戏结束时打印出来。

Generates a random number between 1 and 9 (including 1 and 9). Ask the user the user to get the number, then tell them whether they guessed too low or too high, or exactly right. Extras: keep the game going until the user type ‘exit’ keep track of how many guesses the user has taken, and when the game ends, print this out.

拍不死你 2025-01-08 07:56:07

你好,也许这对你来说没问题,但对其他想尝试的人来说:

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}");

        }
    }
}

Hello maybe it's okay for you but for the other who want to try :

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(
quot;No the number is low than {response} try again !");
                response = Convert.ToInt32(Console.ReadLine());
            }

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

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

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

        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文