random number guessing game

≯℡__Kan透↙ 提交于 2019-12-01 23:27:49

问题


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

回答1:


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

}



回答2:


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



回答3:


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.




回答4:


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 }



回答5:


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



回答6:


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



回答7:


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.



来源:https://stackoverflow.com/questions/9086168/random-number-guessing-game

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!