Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多行,每个C中有6个随机数#_C#_List_While Loop - Fatal编程技术网

C# 多行,每个C中有6个随机数#

C# 多行,每个C中有6个随机数#,c#,list,while-loop,C#,List,While Loop,我正试图写一个程序,你可以输入“x”,并得到“x”行数,每行有6个随机数。我目前的代码给出了“x”数量的相同行的输出,每行有6个随机数。这是到目前为止我的代码 { Random random_generator = new Random(DateTime.Now.Millisecond); int random_numbers, int_games; List<int> lst_numbers = new List<i

我正试图写一个程序,你可以输入“x”,并得到“x”行数,每行有6个随机数。我目前的代码给出了“x”数量的相同行的输出,每行有6个随机数。这是到目前为止我的代码

    {
        Random random_generator = new Random(DateTime.Now.Millisecond);

        int random_numbers, int_games;

        List<int> lst_numbers = new List<int>();

        if (!int.TryParse(txt_input.Text, out int_games))
        {
            MessageBox.Show("Please enter valid number");
            txt_input.Clear();
            txt_input.Focus();
        }
        else
            while (lst_display.Items.Count < int_games)
            {
                while (lst_numbers.Count < 6)
                {
                    random_numbers = random_generator.Next(1, 45);
                    if (!lst_numbers.Contains(random_numbers))
                    {
                        lst_numbers.Add(random_numbers);
                    }
                    else
                    {

                    }
                }
                lst_display.Items.Add(string.Join(",", lst_numbers));
            }
    }
{
随机发生器=新随机(DateTime.Now.毫秒);
int随机数,int游戏;
列表lst_编号=新列表();
如果(!int.TryParse(txt_input.Text,out int_games))
{
MessageBox.Show(“请输入有效数字”);
txt_input.Clear();
txt_input.Focus();
}
其他的
while(lst_display.Items.Count
如果每次都要执行内部循环,当前只执行一次,因为您在第一次迭代时填充了
lst\u编号。您可以将其替换为
for
循环:

while (lst_display.Items.Count < int_games)
{
    for(int i=0; i<6; i++)
    {
         random_numbers = random_generator.Next(1, 45);
         if (!lst_numbers.Contains(random_numbers))
         {
             lst_numbers.Add(random_numbers);
         }
    }
    lst_display.Items.Add(string.Join(",", lst_numbers));
    lst_numbers.Clear();
}
while(lst\u display.Items.Count对于(inti=0;i这不是更容易吗

Public String getList(int numbersPerLine,int rows,int from,int to){
 String list ="";
 for(int i = 1;i<= numbersPerLine;i++;){
   for(int j = 1;j<= rows;j++;){
    List += ""+((int)(Math.random()*to)+from)+",";
   }
   list +=\n";//for starting new line... 
 }
 return list;
}
公共字符串getList(int numbersPerLine、int行、int from、int to){
字符串列表=”;
对于(int i=1;i循环。@Reniuz你怎么漏掉了“identicle”?:)我会解决的up@Leo  :)