3

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();
    }
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • 8
    You have some whiles where you probably need ifs. – Anthony Pegram Jan 31 '12 at 20:10
  • You're falling through to each of the `while` loops each time through, that's why you get multiple prompts printed. You could break out of the `while` loop once you find a match, but it would probably be better to just replace the inner `while` loops with `if` statements. – Cody Gray - on strike Jan 31 '12 at 20:13
  • if this is homework, please add the #homework tag – Muad'Dib Jan 31 '12 at 20:14
  • @Yuck It could be homework, or it could be self-teaching. It may be a simple question to pros, but it's valid. – Phil Jan 31 '12 at 20:15
  • Not sure if this deserves downvotes, it's just a really beginner programming question. We all started somewhere. – Mike Christensen Jan 31 '12 at 20:16
  • is there any way I can use whiles in it instead of ifs –  Jan 31 '12 at 21:03
  • @AsharAslam - The accepted solution only contains two if statements which are required per the requirements you shared. Since this is clearly homework, I am going to tag it as such, based on your previous 5 questions on a different subject. – Security Hound Feb 07 '12 at 20:12

8 Answers8

5

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();
            
    }
Axisnix
  • 2,822
  • 5
  • 19
  • 41
OnResolve
  • 4,016
  • 3
  • 28
  • 50
1

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!");
    }
}
Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102
0

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

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.

0

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

        }
    }
}
Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47
Ananas
  • 1
0

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.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
0

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 }
Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
0

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.");
}
John
  • 6,433
  • 7
  • 47
  • 82