I would like to print out more than six lines of six random numbers, but array bound is blocking me. I have set my array 'numbers' to six in order to always get just six random numbers, but now I dont know what to do to print out for example just ten lines of the numbers, because as I said - I can not due to six bounded array and I do not know how to deal with it. Can anyone help, please?
using System;
class Program
{
static void Main()
{
Draw();
Console.ReadKey();
}
static void Draw()
{
int choice;
Random randomNum = new Random();
int[] numbers = new int[6];
Console.Write("enter line amount: ");
choice = int.Parse(Console.ReadLine());
for (int i = 0; i < choice; i )
{
Console.Write("\n");
for (int j = 0; j < numbers.Length; j )
{
numbers[i] = randomNum.Next(1, 49);
Console.Write("{0} ", numbers[i]);
}
}
}
}
CodePudding user response:
Hy!
In the 2nd for loop, write "j" instead of "i", because the 2nd for loop monitors the length of the array
Try this:
static void Main(string[] args)
{
Draw();
Console.ReadKey();
}
static void Draw()
{
int choice;
Random randomNum = new Random();
int[] numbers = new int[6];
Console.Write("enter line amount: ");
choice = int.Parse(Console.ReadLine());
for (int i = 0; i < choice; i )
{
Console.Write("\n");
for (int j = 0; j < numbers.Length; j )
{
numbers[j] = randomNum.Next(1, 49);
Console.Write("{0} ", numbers[j]);
}
}
}
CodePudding user response:
Arrays in C# are of a fixed size, so I'd recommend using a dynamically-sized collection, such as a List<T>
instead in your case.
There's a good introduction on how to use these on Microsoft's documentation here if you aren't familiar.