时间:2019-03-17 11:58:44

标签: c# arrays user-input

我希望能够创建一个随机整数的数组,其中数组的大小由用户选择,我可以将打印出的数组转移到另一个文本框中。

我尝试过

        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 :(得分:0)

Ypu可以使用以下方法:

您可以使用 Int32.MaxValue 语句来实现max int。

通过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);