0

I'm trying to make a classic lottery style code, where the objective is for the user to choose ten numbers to play with, and then those numbers are compared to a randomly generated number.

So far I got this:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello and Welcome to the Programming Lottery!"); //Just greetings and instructions.
        Console.WriteLine("You'll now get to choose ten different numbers to play with. ");
        Console.WriteLine("Go ahead and type them in.");

        int[] lotteri = new int[10]; //Array to catch ten numbers input.

        for (int i=0; i<lotteri.Length; i++)
            lotteri[i] = int.Parse(Console.ReadLine());

        Console.WriteLine("Very good! Now we'll see if you won anything!");

        Random randomNumber = new Random(1-100);        

        Console.ReadKey();
    }
}

That's as far as I got, I think the array is doing what it should (collecting the user input ten times before moving on).

The MAIN PROBLEM for me now is that I want the program to compare those numbers in the array to a randomly chosen number, and if either of those ten user input numbers match with the randomly generated number to tell the user that they've won (or if it doesn't, that they lost). I just can't seem to get it to work!

Secondary problem, but that I'm trying to learn is handling methods better, so if anyone got any tip on how to use the array in a method and then get it to main, that'd be very appreciated as well!

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
J.SL.
  • 21
  • 3
  • 1
    Look up the Random.Next method – Sebastian Hofmann Dec 05 '17 at 11:11
  • Use another loop to compare each of your array values to random numbers If you want a tip about arrays, don't use them, `List` is way better because they have a dynamical size –  Dec 05 '17 at 11:11
  • Possible duplicate of [How do I generate a random int number in C#?](https://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number-in-c) – Bernhard Barker Dec 05 '17 at 11:20
  • Side note: you should probably validate the user input, if he enters anything other than a number, your code will throw an exception when you try to parse. – Magnetron Dec 05 '17 at 11:27

2 Answers2

1

Simply replace this line Random randomNumber = new Random(1-100); with this :

Random rnd = new Random();//Instanciate a Random object
int randomNumber = rnd.Next(1, 101);//Generate your Random number in range [[1,100]]

Then

Empiric solution (explicit foreach)

foreach (var a in lotteri)
{
    if (a == randomNumber)
    {
        //Handle if the user got the number
        break;
    }
}

Solution 2 (implicit foreach)

if (lotteri.Any(x => x == randomNumber))
    //Handle if the user got the number

Solution 3 (using Contains)

if (lotteri.Contains(randomNumber))
    //Handle if the user got the number

EDIT : Added the suggested solution and increased the range of Next()

  • 4
    _bool win = lotteri.Any(x => x == randomNumber);_ and you can remove the explicit foreach. – Steve Dec 05 '17 at 11:21
  • 2
    Or, `bool win = lotteri.Contains(randomNumber);` Also, the [upper bound is exclusive](https://msdn.microsoft.com/pt-br/library/2dx6wyd4(v=vs.110).aspx), so 100 is not included. – Magnetron Dec 05 '17 at 11:25
0

Program is as follows:

    static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("Hello and Welcome to the Programming Lottery!"); //Just greetings and instructions.
            Console.WriteLine("You'll now get to choose ten different numbers to play with. ");
            Console.WriteLine("Go ahead and type them in.");

            int[] lotteri = new int[10]; //Array to catch ten numbers input.
            for (int i = 0; i < lotteri.Length; i++)
            {
                lotteri[i] = int.Parse(Console.ReadLine());
            }

            Console.WriteLine("Very good! Now we'll see if you won anything!");

            Random rnd = new Random();
            int rnumber = rnd.Next(1, 100);
            bool isWin = false;
            isWin = lotteri.Contains(rnumber);

            Console.WriteLine("Lottery number is::" + rnumber);

            if (isWin)
            {
                Console.WriteLine("Very good! Now we'll see if you won anything!");
            }
            else
            {
                Console.WriteLine("Sorry...Better luck next time!!!");
            }

            Console.ReadKey();
        }
        catch (FormatException ex)
        {
            Console.WriteLine("Only number are allowed.");
        }
        catch
        {
            Console.WriteLine("Something went wrong.");
        }
        Console.ReadKey();
    }
Mittal Patel
  • 2,732
  • 14
  • 23