1

I am putting 5 numbers into an array that are randomly generated. Each array can only have 5 sets of numbers. Depending on how many draws I want, lets say 5 it should print out something like this.

12345 //array 1
54321 //array 2 
98765 //array 3 
45678 //array 4
34589 //array 5 

Then plop it into the List object and print it out.

When I debug the the thing it is working I see the list object being populated with all 5 arrays and the numbers in each element. But when I try to print the list out it just gives me the System.Int32[] output.

Here is my code, terribly written I know but I am just trying to get it to do the basic function, then I plan to clean it up and stuff.

I am basically trying to simulate the Mega Millions lottery kiosk. You select how many draws you want and then 5 sets of randomly generated numbers are printed out. The last statement of:

foreach (int[] g in list)
     Console.WriteLine(" " + g);

This should work I don't know what is wrong and string.join("", g) does not work. It only prints out the first set of numbers in the first array. Believe me I googled this and I cannot find a solution. And since I got the functionality to work via debug, I don't want to waste hours trying to figure out why it won't print it out right.

static void Main(string[] args)
        {

            int[] getLotto = new int[5];  
            int getMega = 0; 
            Program p = new Program();
            Console.WriteLine("MEGA MILLIONS!!\n"); 
            p.result(ref getMega, ref getLotto); 

            for( int i = 0; i < 5; i++)
            {
                Console.Write(getLotto[i] + " "); 
            }

          Console.WriteLine("\n\nMEGA {0}", getMega); 
        }

        void result(ref int Mega, ref int[] storeNumArray)
        {

            var containter = new List<int>(); 
            Random ranNumber = new Random();
            int maxValue = 73;
            int x = 0;
            int y = 0;
            int temp = 0;
            int minValue = 1;
            int draw = 5;
            int[,] nDemension = new int[draw, 5];
            List<int[]> list = new List<int[]>();   
              for(int s = 0; s < draw; s++)
                for (int i = 0; i < 5; i++)
                {
                    x = ranNumber.Next(minValue, maxValue);
                    storeNumArray[i] = x;
                    if (i == 4)
                        list.Add(storeNumArray);  

                }
                while (y < 5)
                {
                    Mega = ranNumber.Next(maxValue);
                    temp = storeNumArray[y];
                    if (Mega != temp)
                    {
                        y = 5;
                        break;
                    }


                }

            foreach (int[] g in list)
                Console.WriteLine(" " + g);
        }

    }
}           
1
  • I don't understand how string.Join(string.Empty, g) cannot work. It does exactly what you need. Console.WriteLine(" " + string.Join(string.Empty, g)); Commented Aug 12, 2017 at 6:44

2 Answers 2

5
foreach (int[] g in list)
{
    Console.WriteLine(" " + g);
}

will not print the full array because ToString() is implicitly called on the array (not the elements of the array), try this:

foreach (int[] g in list)
{
    foreach(int num in g)
    {
        Console.WriteLine(" " + num); //you may want to use String.Join()
    }
}

I am not sure what you are trying to achieve but according to Jonathan's comment you should be doing something like this (again I am not sure if this is what you want):

foreach (int[] g in list)
{
    StringBuilder sb = new StringBuilder();
    foreach(int num in g)
    {
        sb.Append(num.ToString());
    }
    Console.WriteLine(sb.ToString());
}
Sign up to request clarification or add additional context in comments.

7 Comments

Better still would be to have a string variable, fill it with 5 nums and then WriteLine with the string. I'll leave you to correct your answer, rather than write one myself.
No all that does is print the first set of numbers over and over, just like string.join. And I tried to use string.join within your solution same thing. So frustrating my algorithm works but I can't print it out.
Well that did help at least the formatting is right. But it keeps printing the first array over and over. But at least its five rows of numbers.
Ah my algorithm is messed up, your solution is right. I debugged again and each array is getting the same set of numbers in the list..back to the drawing board, But at least the nested for each with the string builder will print out the right format thank you!!
ofcourse you get the same set of numbers. You are adding the same instance of array every time. use a new instance every time.
|
0
for (int s = 0; s < draw; s++)
        {
            for (int i = 0; i < 5; i++)
            {
                x = ranNumber.Next(minValue, maxValue);
                storeNumArray[i] = x;
                if (i == 4)
                {
                    list = new List<int[]>();
                    list.Add(storeNumArray);
                }

            }


            while (y < 5)
            {
                Mega = ranNumber.Next(maxValue);
                temp = storeNumArray[y];
                if (Mega != temp)
                {
                    y = 5;
                    break;
                }


            }

            foreach (int[] g in list)
            {
                StringBuilder sb = new StringBuilder();
                foreach (int num in g)
                {
                    sb.Append(' ' + num.ToString());
                }
                Console.WriteLine(sb.ToString());
            }
        }

5 Comments

Of course a whole bunch of stuff needs to be done, I need to make a constraint so duplicates are not in the the array. It needs to only generate 3 even and odd numbers, the mega number cannot be the same in each draw. Need a new function for that, because I am only generating one mega for five sets. And this is random so want to make a C# windows app so you can enter your own numbers. Thanks everyone! This was a lot of fun. I know get five sets of numbers in the right format that are all random.
The string builder method in addition to the nested foreach loops was clever. I need to debug and understand how that works. Because I had one foreach loop which outputted System.Int32[] but the nested fixed that to outputting numbers. Don't fully understand why.
MEGA MILLIONS!! 17 54 37 43 1 26 23 2 48 26 71 42 25 34 3 19 3 44 62 45 68 51 25 63 14 MEGA 58
In the console each 5 numbers is in its own row. Dont know why the website won't display it that way.
After running 10,000 draws the Mega number was duplicated, got to work on that problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.