Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random Generator Questions

Tags:

c#

How do i make a application that will produce a random number between 1-10 and then ask the user to guess that number, and if that number is matching the random number tell them if they got it right!

I'm a student, like super new and was out of class for a few days because of surgery and i can not figure this out for the life of me!

This is what i've come up with for the problem.

namespace GuessingGame
{
   class Program
   {
        static void Main(string[] args)
        {
            int min = 1;
            int max = 10;    
            Random ranNumberGenerator = new Random();
            int randomNumber;
            randomNumber = ranNumberGenerator.Next(min, max);
            Console.WriteLine("Guess a random number between 1 and 10.");
            Console.ReadLine();
            if (randomNumber == randomNumber)
                Console.WriteLine("You got it!");
            else
                Console.WriteLine("Sorry, try again!");
        }
    }
}
like image 994
user3314948 Avatar asked Feb 16 '14 02:02

user3314948


People also ask

What is a random question generator?

Random question generators are tools that produce questions on demand. For example, you may want to generate questions for icebreakers, games of twenty questions or to facilitate conversations and get to know someone.


1 Answers

The upper limit for the Next method is exclusive, so you want to use 11 rather than 10 for that:

randomNumber = ranNumberGenerator.Next(min, max + 1);

You are ignoring the input from the user. Capture that in a string:

string input = Console.ReadLine();

Then parse the input to a number:

int number = Int32.Parse(input);

Now you can compare that number to the random number.


If you want to handle incorrect input from the user in a more user friendly way than crashing, you would use TryParse to attempt to parse the number.

like image 197
Guffa Avatar answered Oct 29 '22 23:10

Guffa