C # пользовательский размер ввода - PullRequest
0 голосов
/ 17 марта 2019

Я хочу иметь возможность создавать массив со случайными целыми числами, размер которого выбирается пользователем, и я могу перенести распечатанный массив в другое текстовое поле.

я пытался

        int listamount; //stores the number

        if (int.TryParse(LStextbox.Text, out listamount) && LStextbox.Text.Length > 0)
        {
            //int.tryparse converts the string into a integer
            //text.lentgh > 0 makes sure the box will not be left blank

        }
        else
        {

        }
        int min = 0;
        int max = 100; //i want to make the max an indefinite number, is that posible?

        int num = listamount;
        Random r = new Random();

        int[] ar;
        ar = new int[num];
        for (i = 0; int =< num - 1; i++)
        {
            ar[i] = r.Next(min, max);
        }

1 Ответ

0 голосов
/ 17 марта 2019

Ypu может использовать следующий подход:

Вы можете достичь максимального значения int, используя Int32.MaxValue оператор.

Кстати, у вас есть ошибки в цикле for, я их исправил.

int listamount; //stores the number
if (int.TryParse(LStextbox.Text, out listamount) && LStextbox.Text.Length > 0)
{
    //int.tryparse converts the string into a integer
    //text.lentgh > 0 makes sure the box will not be left blank

}
else
{

}
int min = 0;
int max = Int32.MaxValue; //i want to make the max an indefinite number, is that posible?

int num = listamount;
Random r = new Random();

int[] ar;
ar = new int[num];
for (int i = 0; i <= num - 1; i++)
{
    ar[i] = r.Next(min, max);
}
foreach(int y in ar)
    Console.WriteLine(y);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...