Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to create a software (console application) for lottery games with numbers (1-39), The functionalities are:
Initially play players by selecting seven numbers
The software generates seven winning numbers randomly
The software automatically, after the end of the game, must list the players winners having more than three goals, and present the number them.

What I have tried:

C#
class Program
    {
        private static void InitArray()
        {
            int[] arr = new int[7];
            Random rnd = new Random();
            int tmp;

            for (int i = 0; i < arr.Length; i++)
            {
                tmp = rnd.Next(1, 39);
                while (IsDup(tmp,arr))
                {
                    tmp = rnd.Next(1, 39);
                }
                arr[i] = tmp;
            }           
            PrintTheArray(arr);
        }
        private static void PrintTheArray(int[] arr)
        {
            foreach (var item in arr)
            {
                Console.WriteLine(item);
            }
        }
        private static bool IsDup(int tmp, int[] arr)
        {
            foreach (var item in arr)
            {
                if(item==tmp)
                {
                    return true;
                }
            }
            return false;
        }


        static void Main(string[] args)
        {
                
                //User Input
                List<int> lista1 = new List<int>();
            for (int i = 1; i <= 7; i++)
            {
                try
                {
                    Console.Write("First Number {0}: ", i);
                    int x = Convert.ToInt16(Console.ReadLine());

                    lista1.Add(x);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("The input number is incorret! It has to be whole number");
                    Console.WriteLine("Error: {0}", ex.Message);
                    i--;
                }
            }
            Console.WriteLine("\nChoosen Numbers are: ");
            foreach (int item in lista1)
            {
                Console.WriteLine(item);
            }

            //Random Number Generator
            Console.WriteLine("Random Numbers are: ");
            InitArray();
            Console.ReadLine();
        }
Posted
Updated 27-May-19 5:32am
v2
Comments
[no name] 25-May-19 13:56pm    
And now what?
Patrice T 25-May-19 14:05pm    
Ok, what is the question ?

1 solution

Don't do it like that!
Generate an array and fill it with 39 elements: the numbers 1 to 39 inclusive.
Then write a method which scrambles them: generate 500 or 5000 pairs of numbers between 0 and 38 and swap those two elements.
The first "n" elements of the array are the winning numbers,so copy them into an array of "n" elements, and sort them.

Now get the user to enter "n" numbers, sort them, and check for duplicates. If they are all fine, check for winning numbers. (That's easy - the two arrays are sorted, so that's nice and easy to do!)

Stop using names like "print the array" and use names which reflect why they do that: "ShowWinningNumbers" for example means your code starts to explain what it is doing and why for you, and that makes it easier for everyone - including you! - to understand and maintain it.

Basically, make your app reflect what the "real world" lottery does instead of how the language works. That way, it's much easier to write!
 
Share this answer
 
Comments
Richard MacCutchan 26-May-19 8:32am    
Quote:make your app reflect what the "real world"
Reminds me of a Cobol program I once had to work on which contained statements like:
IF (JAIL IS FULL)   MOVE JESSE_JAMES TO DODGE_CITY.

:laugh:
Patrice T 27-May-19 10:37am    
LOL
Qendrim Istrefi 26-May-19 20:55pm    
If you have better idea of solution can you write it than?
OriginalGriff 27-May-19 2:10am    
Yes I can.
But ... no, I won't.
This is your homework, not mine - and you will learn nothing if I do it for you.
Give it a try: this isn't complicated really!
Qendrim Istrefi 27-May-19 10:36am    
Yeah Im working on it but I need help.. if this website isnt for helping you out than why exist?? and you can see my code but I need a final step to connect input value in console application with information about player..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900