Home > other >  My if statements don't work the way I expect them to (C#)
My if statements don't work the way I expect them to (C#)

Time:12-18

I am creating a number-guessing in C#, and I am using if statements in it. This is my code:

namespace number guessing            
{
    class Program
    {
        public static void Main()
        {
            Console.WriteLine("Welcome to the Number Guessing Game!");
            Console.WriteLine("You have to guess the number the computer has chosen, ranging from 1 to 100.");
            Console.WriteLine("Good Luck!");
            Random gen = new Random(1 - 100);
            int number1 = gen.Next();
            int number2 = gen.Next();
            int userGuess = Convert.ToInt32(Console.ReadLine());
            if (userGuess < number1)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Too large! Guess a smaller number!");
                Console.WriteLine("Guess again!");
                Convert.ToInt32(Console.ReadLine());
                Console.ForegroundColor = ConsoleColor.Black;
            }
            if (userGuess > number1)
            {
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("Too small! Guess a larger number!");
                Console.WriteLine("Guess again!");
                Convert.ToInt32(Console.ReadLine());
                Console.ForegroundColor = ConsoleColor.Black;
            }
            if (userGuess == number1)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Well done! You found the secret number!");
            }
        }
    }
}

When I run the code, it only shows "Too large! Guess a smaller number!". I do not know how to fix this and Visual Studios also says that there is no error so... I would like help, please.

Note: I don't mind any tips on making this code a little bit more condensed!

CodePudding user response:

This this is the cause of your issue:

Random gen = new Random(1 - 100);

It is the equivalent of this:

Random gen = new Random(-99);

Which says to seed the pseudo random number generator with the seed of -99. That means that it will always return the same pseudo random numbers each time it runs.

So when you run these lines:

int number1 = gen.Next();
int number2 = gen.Next();

...you always get the same numbers.

Now, I my computer I get these numbers:

958527983
1859223698

The reason they are so large is that Random.Next() returns a non-negative random integer. So inclusive between the value of 0 and 2147483647.

If you want to generate a number between 1 and 100 then you need this code:

Random gen = new Random();
int number = gen.Next(1, 101);

Here's the full code to implement your guessing game:

Console.WriteLine("Welcome to the Number Guessing Game!");
Console.WriteLine("You have to guess the number the computer has chosen, ranging from 1 to 100.");
Console.WriteLine("Good Luck!");
Random gen = new Random();
int number = gen.Next(1, 101);
while (true)
{
    int userGuess = Convert.ToInt32(Console.ReadLine());
    if (userGuess > number)
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("Too large! Guess a smaller number!");
    }
    else if (userGuess < number)
    {
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("Too small! Guess a larger number!");
    }
    else
    {
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("Well done! You found the secret number!");
        break;
    }
    Console.WriteLine("Guess again!");
    Console.ForegroundColor = ConsoleColor.Black;
}

CodePudding user response:

public static void Main()
        {
            Console.WriteLine("Welcome to the Number Guessing Game!");
            Console.WriteLine("You have to guess the number the computer has chosen, ranging from 1 to 100.");
            Console.WriteLine("Good Luck!");
            Random gen = new Random();//dont pass paramter for range here 
            int number1 = gen.Next(1,100);//pass range parameter here  
            //int number2 = gen.Next(1,100);
            int userGuess = Convert.ToInt32(Console.ReadLine());
            if (userGuess < number1)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Too small! Guess a larger number!");
                Console.WriteLine("Guess again!");
               userGuess= Convert.ToInt32(Console.ReadLine());
                Console.ForegroundColor = ConsoleColor.Black;
            }
            if (userGuess > number1)
            {
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("Too large! Guess a smaller number!");
                Console.WriteLine("Guess again!");
                userGuess = Convert.ToInt32(Console.ReadLine());
                Console.ForegroundColor = ConsoleColor.Black;
            }
            if (userGuess == number1)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Well done! You found the secret number!");
            }
            Console.WriteLine("Your Attempt is Over..Please REStart Game !");
            Console.ReadKey();
        }
    }
  • Related