我正在尝试编写一个程序,您可以输入“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));
}
}
答案 0 :(得分:0)
你想每次执行内循环,目前它只执行一次,因为你在第一次迭代时填充lst_numbers
。你可以用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();
}
答案 1 :(得分:0)
这不容易吗?
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;
}