4 Replies - 1676 Views - Last Post: 17 December 2012 - 02:26 PM Rate Topic: -----

#1 TheTiger007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-December 12

Console application lottery game

Posted 15 December 2012 - 04:58 PM

Hello, I just signed up for this forum because I'm an IT student and we program in C# and I would like to get better at it. I've gotten an assignment to make a console program but I'm having a hard time making it. So the assignment is something like a lottery game. It contains randomizers and huge loops and I keep getting errors (I'm still kind of a newb at programming). So this is how it should work.

Step 1: You give 6 numbers between 1 and 42
Posted Image

Step 2: The program goes through all possible outcomes untill all 6 numbers appear at the same time
Posted Image

Step 3: It shows how many times the program needed to guess the right numbers



So my big problem is at the randomizing part. When I make a randomizer for the numbers, all 6 coloms show the same random number and I don't know what to do. I would greatly appreciate someone's help! Thank you :)/>/>

This post has been edited by modi123_1: 15 December 2012 - 05:14 PM
Reason for edit:: image tages are for images not media tags


Is This A Good Question/Topic? 0
  • +

Replies To: Console application lottery game

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Console application lottery game

Posted 15 December 2012 - 05:26 PM

Post your code on how you are doing the random numbers. My guess is you are creating multiple Random objects each time you go through your loop, which is the wrong way to deal with Random objects.
Was This Post Helpful? 0
  • +
  • -

#3 TheTiger007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-December 12

Re: Console application lottery game

Posted 16 December 2012 - 09:45 AM

View PostMomerath, on 15 December 2012 - 05:26 PM, said:

Post your code on how you are doing the random numbers. My guess is you are creating multiple Random objects each time you go through your loop, which is the wrong way to deal with Random objects.


This is what i've got so far

  {
            Random getal = new Random();
            int[] lottotrekking = new int[6];

            Console.WriteLine("Geef je geluksgetallen in <tussen 1 en 42>");
            Console.WriteLine("Geef je eerste getal in");
            int getal1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Geef je tweede getal in");
            int getal2 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Geef je derde getal in");
            int getal3 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Geef je vierde getal in");
            int getal4 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Geef je vijfde getal in");
            int getal5 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Geef je zesde getal in");
            int getal6 = Convert.ToInt32(Console.ReadLine());

            while (getal1 != lottotrekking[0] || getal2 != lottotrekking[1] || getal3 != lottotrekking[2] || getal4 != lottotrekking[3] || getal5 != lottotrekking[4] || getal5 != lottotrekking[4] || getal6 != lottotrekking[5])
            {

                for (int i = 0; i < lottotrekking.Length; i++)
                {
                    int cijfer = getal.Next(1, 43);
                    lottotrekking[i] = cijfer;
                    Console.WriteLine(lottotrekking[0] + "\t " + lottotrekking[1] + "\t " + lottotrekking[2] + "\t " + lottotrekking[3] + "\t " + lottotrekking[4] + "\t " + lottotrekking[5]);
                }
            }

            if (getal1 == lottotrekking[0] || getal2 == lottotrekking[1] || getal3 == lottotrekking[2] || getal4 == lottotrekking[3] || getal5 == lottotrekking[4] || getal5 == lottotrekking[4] || getal6 == lottotrekking[5])
            {
                Console.WriteLine(lottotrekking[0] + " " + lottotrekking[1] + " " + lottotrekking[2] + " " + lottotrekking[3] + " " + lottotrekking[4] + " " + lottotrekking[5]);
            }

            Console.ReadLine();
        }


This thing goes on endlessly, any idea how i can fix this?
Was This Post Helpful? 0
  • +
  • -

#4 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: Console application lottery game

Posted 17 December 2012 - 05:53 AM

The rows value are repeating because you have your print statement inside the for loop. So what's happening is that the randomized value for a specific index is printed each iteration. Placing the print statement outside the for loop will fix this.

Also, I suggest placing the if statement that checks if all the randomized numbers are equal to the input numbers INSIDE the for loop then place a break statement at the end so it would break out of the loop if true.
Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Console application lottery game

Posted 17 December 2012 - 02:26 PM

You also do no ordering of the numbers so you must generate them in the exact same order that they were entered. For example, if the user entered "1, 2, 3, 4, 5, 6" and your random generation was "2,3,4,5,6,1" you would count that as a miss.

Put their requested numbers in an array. Sort the array. You are already putting the random numbers into an array, so sort it. Then use a loop to compare rather than that ugly if statement.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1