0

I have this exercise:

  1. Make a method that generates a random number between 1 and 100.

  2. Ask the user to guess a number between 1 and 100.

  3. Make a method that checks if the user's guess is equal to the random number that has been generated. This method will also say "higher" or "lower" until the user guesses the random number.

I have no problem with steps 1 and 2, but I'm having problems with the 3rd step. Here is my code so far:

static void Main(string[] args)
{
    int randomNumber = GenerateRandomNumberMethod();
    Console.WriteLine("Guess a number between 1 and 100:");                
}

// generate random number method (step 2)   
public static int GenerateRandomNumberMethod()
{
    Random rdn = new Random();
    int random = rd.Next(1,100);
    return random;
}

//step 3 (Method to compare guessed number with random number)?

I hope it is not a stupid question, the answer is probably pretty simple.

Antonio
  • 1
  • 3
  • so main problem for you how to read number, that user input into console. – demo Oct 08 '18 at 15:41
  • You'll need to receive the user input inside a loop using `Console.ReadLine()` and converting that input into a number; don't forget the user can enter non-numeric values – Rubens Farias Oct 08 '18 at 15:42
  • You'll need to use [`Console.ReadLine()`](https://stackoverflow.com/q/6825943/4934172) to get the user input, use [`int.TryParse`](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number) to make sure the user entered an integer number, (and get it), then use a [`while` loop](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/while) to repeat these steps, which would be something like `while (userNumber != randomNumber) { // Do stuff }`. – 41686d6564 stands w. Palestine Oct 08 '18 at 15:42
  • 4
    Good effort so far. You will need to make use of a loop that checks the condition of the user input against `randomNumber`. Research `do/while` and `Console.ReadLine`. – Chris Pickford Oct 08 '18 at 15:42
  • See: [while (C# Reference)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/while). – Olivier Jacot-Descombes Oct 08 '18 at 15:43
  • Also, `rd.Next(1,100)` wont give you the result you think it will... – maccettura Oct 08 '18 at 15:43
  • You also should consider making your `rnd` object a class-level variable rather than creating a new one at the same time. If someone were to call your method inside a tight loop, it would return the same number for many results since `Random` is seeded (initialized) based on the system clock. Near the top of your class you could have `private static Random Rnd = new Random();` and then you can do `int random = Rnd.Next(1, 100);` instide your method. – Rufus L Oct 08 '18 at 16:07
  • Also a possible duplicate: [Random Number Guessing Game C#](https://stackoverflow.com/q/24604642) and [C# Guess random number](https://stackoverflow.com/q/46665006). – dbc Oct 08 '18 at 17:21

2 Answers2

3

Well, the first thing you want to do is to store the generated random number in a variable, so that it will not be re-generated after each time the user attempts.

The second thing you want to do is to create a method that will compare the user input against the randomly-generated number, and return a value indicating if the user input is smaller, equal, or higher than the generated number (A bool? might be a good choice for this).

The third thing you need is a safe way to convert the string the user entered to an int.
Do not make the common mistake of using Convert.ToInt32 - use int.TryParse instead. You do not want to throw an exception if the user entered Zohar instead of 123.

The fourth thing to do is to implement a loop - that will keep getting inputs from the user and tell them if the number is too high or too low. Exit the loop if the number is the same as the generated random number.

The last thing to do is to tell the user they finally guessed the correct number and exit (or maybe restart) the game.

Since it's clearly a homework assignment, I'll leave the coding part for you to handle, because you will not learn anything if you don't at least do it yourself, but at least now you have a plan.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
0

Try

int userGuess = int.Parse(Console.ReadLine());

See this previous question on reading user input.

See this previous question on your exact problem.

See comments for note on int.Parse()

Patrick
  • 419
  • 4
  • 16
  • I need to make another method, to compare the user input to the generated number. This method also has to say higher or lower, untill the user guesses the number. – Antonio Oct 08 '18 at 15:45
  • 1
    **Never ever** do `int.Parse(Console.ReadLine());`. If the user enters anything that can't be parsed to an int, you will get an exception. **Always** prefer `TryParse` over `Parse` with user input. – Zohar Peled Oct 08 '18 at 15:50
  • @Antonio - what problem are you having with comparing the two numbers? If you post some examples of things you've tried, we can correct it. Also see this article [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822) – Robin Bennett Oct 08 '18 at 16:13